// File: /workspace/github_c_makefile_star2_O1/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 0x4096(%rip), %rdi      # 0xe45d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3170
movaps 0x4038(%rip), %xmm0     # 0xe410
movaps %xmm0, 0xb6e1(%rip)     # 0x15ac0
leaq 0xb6da(%rip), %rdx      # 0x15ac0
xorl %ebp, %ebp
xorl %eax, %eax
movq %rbx, (%rsp)
movzbl (%rbx,%rbp), %ecx
testl %ecx, %ecx
setne %r14b
cmpl $0x2c, %ecx
je 0xa405
testl %ecx, %ecx
jne 0xa489
movb $0x0, (%rbx,%rbp)
movslq %eax, %r15
addq %rbx, %r15
movzbl (%r15), %eax
cmpl $0x20, %eax
je 0xa41d
cmpl $0x9, %eax
jne 0xa422
incq %r15
jmp 0xa40f
movq %rdx, %rbx
leaq 0x4041(%rip), %rdi      # 0xe46d
movq %r15, %rsi
xorl %eax, %eax
callq 0x3170
cmpl $0x5, 0xb673(%rip)      # 0x15ab0
jge 0xa539
movq %r15, %rdi
callq 0x3270
testq %rax, %rax
je 0xa48e
movq %rax, %r12
movl 0xb656(%rip), %r15d     # 0x15ab0
movslq %r15d, %r13
movq %r13, %rax
shlq $0x4, %rax
leaq (%rax,%rbx), %rdi
addq $0x4, %rdi
movq 0x18(%r12), %rax
movq (%rax), %rsi
movslq 0x14(%r12), %rdx
callq 0x32c0
movq %rbx, %rdx
movzwl 0x10(%r12), %eax
jmp 0xa4bb
movb $0x1, %r14b
jmp 0xa4de
movq %r15, %rdi
callq 0x3240
cmpl $-0x1, %eax
je 0xa53e
movl 0xb60a(%rip), %r15d     # 0x15ab0
movslq %r15d, %r13
movq %r13, %rcx
shlq $0x4, %rcx
movq %rbx, %rdx
movl %eax, 0x4(%rcx,%rbx)
movw $0x2, %ax
shlq $0x4, %r13
movw %ax, (%r13,%rdx)
movw $0x611e, 0x2(%r13,%rdx) # imm = 0x611E
incl %r15d
movl %r15d, 0xb5d9(%rip)     # 0x15ab0
leal 0x1(%rbp), %eax
movq (%rsp), %rbx
incq %rbp
testb %r14b, %r14b
jne 0xa3ee
movq $0x621e0002, 0x8(%rsp)  # imm = 0x621E0002
movl $0x2, %edi
movl $0x2, %esi
movl $0x11, %edx
callq 0x3510
movl %eax, 0xb5a7(%rip)      # 0x15ab4
cmpl $-0x1, %eax
je 0xa547
leaq 0x8(%rsp), %rsi
movl %eax, %edi
movl $0x10, %edx
callq 0x33c0
cmpl $-0x1, %eax
je 0xa54c
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3913
movq (%rsp), %rdi
callq 0x38ef
callq 0x394c
callq 0x3937
nopl (%rax)