// File: /workspace/github_c_makefile_star2_O2/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 0x4a61(%rip), %rdi      # 0xe49d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3170
movaps 0x4a03(%rip), %xmm0     # 0xe450
movaps %xmm0, 0xc07c(%rip)     # 0x15ad0
xorl %ebp, %ebp
movq %rbp, %rax
incq %rbp
movzbl -0x1(%rbx,%rbp), %r14d
testl %r14d, %r14d
je 0x9a72
cmpl $0x2c, %r14d
je 0x9a72
incq %rbp
jmp 0x9a5c
movb $0x0, -0x1(%rbx,%rbp)
movslq %eax, %r15
addq %rbx, %r15
movzbl (%r15), %eax
cmpl $0x20, %eax
je 0x9a8b
cmpl $0x9, %eax
jne 0x9a90
incq %r15
jmp 0x9a7d
leaq 0x4a16(%rip), %rdi      # 0xe4ad
movq %r15, %rsi
xorl %eax, %eax
callq 0x3170
cmpl $0x5, 0xc018(%rip)      # 0x15ac0
jge 0x9b9e
movq %r15, %rdi
callq 0x3280
testq %rax, %rax
je 0x9aff
movq %rax, %r12
movl 0xbffb(%rip), %r15d     # 0x15ac0
movslq %r15d, %r13
movq %r13, %rax
shlq $0x4, %rax
leaq 0xbffa(%rip), %rcx      # 0x15ad0
leaq (%rax,%rcx), %rdi
addq $0x4, %rdi
movq 0x18(%r12), %rax
movq (%rax), %rsi
movslq 0x14(%r12), %rdx
callq 0x32d0
leaq 0xbfd9(%rip), %rcx      # 0x15ad0
movzwl 0x10(%r12), %eax
jmp 0x9b33
movq %r15, %rdi
callq 0x3250
cmpl $-0x1, %eax
je 0x9bbb
movl 0xbfa9(%rip), %r15d     # 0x15ac0
movslq %r15d, %r13
movq %r13, %rcx
shlq $0x4, %rcx
leaq 0xbfa8(%rip), %rdx      # 0x15ad0
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, 0xbf71(%rip)     # 0x15ac0
testb %r14b, %r14b
jne 0x9a56
movq $0x621e0002, 0x8(%rsp)  # imm = 0x621E0002
pushq $0x2
popq %rdi
pushq $0x11
popq %rdx
movl %edi, %esi
callq 0x3530
movl %eax, 0xbf50(%rip)      # 0x15ac4
cmpl $-0x1, %eax
je 0x9bde
leaq 0x8(%rsp), %rsi
pushq $0x10
popq %rdx
movl %eax, %edi
callq 0x33e0
cmpl $-0x1, %eax
je 0x9be7
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8453(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x490e(%rip), %rsi      # 0xe4bd
pushq $0x5
popq %rdx
xorl %eax, %eax
callq 0x32a0
jmp 0x9bd6
movq 0x8436(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x490d(%rip), %rsi      # 0xe4d9
movq %rbx, %rdx
xorl %eax, %eax
callq 0x32a0
pushq $-0x1
popq %rdi
callq 0x34a0
leaq 0x490a(%rip), %rdi      # 0xe4ef
jmp 0x9bee
leaq 0x491c(%rip), %rdi      # 0xe50a
callq 0x3450
jmp 0x9bd6
nopl (%rax)