// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl $0x0, -0x14(%rbp)
movw $0x1e61, -0x16(%rbp)    # imm = 0x1E61
movq -0x8(%rbp), %rsi
leaq 0x49bd(%rip), %rdi      # 0x114f1
movb $0x0, %al
callq 0x3170
leaq 0xb00e(%rip), %rdi      # 0x17b50
movl $0x5, %esi
movl $0x10, %edx
callq 0x31b0
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x8(%rbp), %rax
movslq -0x20(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x21(%rbp)
movsbl -0x21(%rbp), %eax
cmpl $0x2c, %eax
je 0xcb83
movsbl -0x21(%rbp), %eax
cmpl $0x0, %eax
jne 0xcdb3
movq -0x8(%rbp), %rax
movslq -0x20(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x8(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movsbl (%rax,%rcx), %ecx
movb $0x1, %al
cmpl $0x20, %ecx
movb %al, -0x35(%rbp)
je 0xcbba
movq -0x8(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x9, %eax
sete %al
movb %al, -0x35(%rbp)
movb -0x35(%rbp), %al
testb $0x1, %al
jne 0xcbc3
jmp 0xcc01
movq -0x8(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0xcbf6
movq 0x7415(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x491c(%rip), %rsi      # 0x11501
movb $0x0, %al
callq 0x3290
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xcb8f
movq -0x8(%rbp), %rsi
movslq -0x1c(%rbp), %rax
addq %rax, %rsi
leaq 0x4907(%rip), %rdi      # 0x1151a
movb $0x0, %al
callq 0x3170
cmpl $0x5, 0xaf1f(%rip)      # 0x17b40
jl 0xcc4a
movq 0x73c6(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x48f6(%rip), %rsi      # 0x1152a
movl $0x5, %edx
movb $0x0, %al
callq 0x3290
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
movq -0x8(%rbp), %rdi
movslq -0x1c(%rbp), %rax
addq %rax, %rdi
callq 0x3270
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0xcd16
movq -0x8(%rbp), %rdi
movslq -0x1c(%rbp), %rax
addq %rax, %rdi
callq 0x3240
movl %eax, -0x14(%rbp)
cmpl $-0x1, -0x14(%rbp)
jne 0xcca8
movq 0x7367(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
movq -0x8(%rbp), %rdx
leaq 0x48af(%rip), %rsi      # 0x11546
movb $0x0, %al
callq 0x3290
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
movl -0x14(%rbp), %ecx
movslq 0xae8e(%rip), %rdx      # 0x17b40
leaq 0xae97(%rip), %rax      # 0x17b50
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movslq 0xae76(%rip), %rcx      # 0x17b40
leaq 0xae7f(%rip), %rax      # 0x17b50
shlq $0x4, %rcx
addq %rcx, %rax
movw $0x2, (%rax)
movzwl -0x16(%rbp), %edi
callq 0x3160
movw %ax, %cx
movslq 0xae50(%rip), %rdx      # 0x17b40
leaq 0xae59(%rip), %rax      # 0x17b50
shlq $0x4, %rdx
addq %rdx, %rax
movw %cx, 0x2(%rax)
movl 0xae38(%rip), %eax      # 0x17b40
addl $0x1, %eax
movl %eax, 0xae2f(%rip)      # 0x17b40
jmp 0xcd9d
movslq 0xae23(%rip), %rax      # 0x17b40
leaq 0xae2c(%rip), %rdi      # 0x17b50
shlq $0x4, %rax
addq %rax, %rdi
addq $0x4, %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movslq 0x14(%rax), %rdx
callq 0x32c0
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movw %ax, %cx
movslq 0xade8(%rip), %rdx      # 0x17b40
leaq 0xadf1(%rip), %rax      # 0x17b50
shlq $0x4, %rdx
addq %rdx, %rax
movw %cx, (%rax)
movzwl -0x16(%rbp), %edi
callq 0x3160
movw %ax, %cx
movslq 0xadc4(%rip), %rdx      # 0x17b40
leaq 0xadcd(%rip), %rax      # 0x17b50
shlq $0x4, %rdx
addq %rdx, %rax
movw %cx, 0x2(%rax)
movl 0xadac(%rip), %eax      # 0x17b40
addl $0x1, %eax
movl %eax, 0xada3(%rip)      # 0x17b40
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movsbl -0x21(%rbp), %eax
cmpl $0x0, %eax
jne 0xcdb1
jmp 0xcdc3
jmp 0xcdb3
jmp 0xcdb5
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xcb5f
movw $0x2, -0x34(%rbp)
movl $0x0, -0x30(%rbp)
movzwl -0x16(%rbp), %eax
addl $0x1, %eax
movzwl %ax, %edi
callq 0x3160
movw %ax, -0x32(%rbp)
movl $0x2, %esi
movl $0x11, %edx
movl %esi, %edi
callq 0x3510
movl %eax, 0xad4a(%rip)      # 0x17b44
cmpl $-0x1, 0xad43(%rip)     # 0x17b44
jne 0xce19
leaq 0x4752(%rip), %rdi      # 0x1155c
callq 0x3430
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
movl 0xad25(%rip), %edi      # 0x17b44
leaq -0x34(%rbp), %rsi
movl $0x10, %edx
callq 0x33c0
cmpl $-0x1, %eax
jne 0xce48
leaq 0x473e(%rip), %rdi      # 0x11577
callq 0x3430
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
xorl %eax, %eax
addq $0x40, %rsp
popq %rbp
retq