// File: /workspace/github_c_makefile_star2_O3/Matthias-Wandel[P]imgcomp/src/send_udp.c
// Function: InitUDP
int InitUDP(char * HostNames)
{
    #ifdef _WIN32
        WSADATA wsaData;
    #endif

    struct hostent * hp;
    unsigned int addr=0;
    unsigned short PortNum = MAGIC_PORTNUM; // Use a different port, leave this for listener

    //-------------------------------------------------------------------
    // Resolve the remote address
    printf("Init UDP to %s\n",HostNames);

    memset(&dests,MAX_DESTS,sizeof(struct sockaddr_in));

    int s = 0;
    for (int a=0;;a++){
        char c = HostNames[a];
        if (c == ',' || c == '\0'){
            HostNames[a] = '\0';
            while (HostNames[s] == ' ' || HostNames[s] == '\t'){
                if (HostNames[s] == '\0'){
                    fprintf(stderr, "Empty hostname specified");
                    exit(-1);
                }
                s += 1;
            }

            printf("Hostname: '%s'\n",HostNames+s);
            if (NumDests >= MAX_DESTS){
                fprintf(stderr, "Maximum %d UDP destinations",MAX_DESTS);
                exit(-1);
            }

            hp = gethostbyname(HostNames+s);

            if (hp == NULL){
                // If address is not a host name, assume its a x.x.x.x format address.
                addr = inet_addr(HostNames+s);
                if (addr == INADDR_NONE) {
                    // Not a number format address either.  Give up.
                    fprintf(stderr,"Unable to resolve %s\n",HostNames);
                    exit(-1);
                }
                dests[NumDests].sin_addr.s_addr = addr;
                dests[NumDests].sin_family = AF_INET;
                dests[NumDests].sin_port = htons(PortNum);
                NumDests += 1;

            }else{
                // Resolved to a host name.
                memcpy(&(dests[NumDests].sin_addr),hp->h_addr,hp->h_length);
                dests[NumDests].sin_family = hp->h_addrtype;
                dests[NumDests].sin_port = htons(PortNum);
                NumDests += 1;
            }

            // Look for more host names in string
            s = a+1;
            if (c == '\0') break;
        }
    }

    //-------------------------------------------------------------------
    // Open socket (though we don't need to receive any packets, just send)
    {
        struct sockaddr_in local;

        local.sin_family = AF_INET;
        local.sin_addr.s_addr = INADDR_ANY;

        // Use a different port so pan program can still use this port.
        local.sin_port = htons(PortNum+1);
        sockUDP = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

        if (sockUDP == INVALID_SOCKET){
            perror("couldn't create UDP socket");
            exit(-1);
        }

        if (bind(sockUDP,(struct sockaddr*)&local,sizeof(local) ) == SOCKET_ERROR) {
            perror("UDP socket open failed");
            exit(-1);
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
leaq 0x4115(%rip), %rdi      # 0xe4cd
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3170
movaps 0x40b7(%rip), %xmm0     # 0xe480
movaps %xmm0, 0xb6f0(%rip)     # 0x15ac0
xorl %ebp, %ebp
movq %rbp, %rax
incq %rbp
movzbl -0x1(%rbx,%rbp), %r14d
testl %r14d, %r14d
je 0xa3ee
cmpl $0x2c, %r14d
je 0xa3ee
incq %rbp
jmp 0xa3d8
movb $0x0, -0x1(%rbx,%rbp)
movslq %eax, %r15
addq %rbx, %r15
movzbl (%r15), %eax
cmpl $0x20, %eax
je 0xa407
cmpl $0x9, %eax
jne 0xa40c
incq %r15
jmp 0xa3f9
leaq 0x40ca(%rip), %rdi      # 0xe4dd
movq %r15, %rsi
xorl %eax, %eax
callq 0x3170
cmpl $0x5, 0xb68c(%rip)      # 0x15ab0
jge 0xa523
movq %r15, %rdi
callq 0x3280
testq %rax, %rax
je 0xa47b
movq %rax, %r12
movl 0xb66f(%rip), %r15d     # 0x15ab0
movslq %r15d, %r13
movq %r13, %rax
shlq $0x4, %rax
leaq 0xb66e(%rip), %rcx      # 0x15ac0
leaq (%rax,%rcx), %rdi
addq $0x4, %rdi
movq 0x18(%r12), %rax
movq (%rax), %rsi
movslq 0x14(%r12), %rdx
callq 0x32d0
leaq 0xb64d(%rip), %rcx      # 0x15ac0
movzwl 0x10(%r12), %eax
jmp 0xa4af
movq %r15, %rdi
callq 0x3250
cmpl $-0x1, %eax
je 0xa528
movl 0xb61d(%rip), %r15d     # 0x15ab0
movslq %r15d, %r13
movq %r13, %rcx
shlq $0x4, %rcx
leaq 0xb61c(%rip), %rdx      # 0x15ac0
movl %eax, 0x4(%rcx,%rdx)
movq %rdx, %rcx
movw $0x2, %ax
shlq $0x4, %r13
incl %r15d
movw %ax, (%r13,%rcx)
movw $0x611e, 0x2(%r13,%rcx) # imm = 0x611E
movl %r15d, 0xb5e5(%rip)     # 0x15ab0
testb %r14b, %r14b
jne 0xa3d2
movq $0x621e0002, 0x8(%rsp)  # imm = 0x621E0002
movl $0x2, %edi
movl $0x2, %esi
movl $0x11, %edx
callq 0x3520
movl %eax, 0xb5bd(%rip)      # 0x15ab4
cmpl $-0x1, %eax
je 0xa530
leaq 0x8(%rsp), %rsi
movl %eax, %edi
movl $0x10, %edx
callq 0x33d0
cmpl $-0x1, %eax
je 0xa535
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x394d
movq %rbx, %rdi
callq 0x3929
callq 0x3914
callq 0x38ff
nop