// File: /workspace/github_c_makefile_star2_O0/pencode-lab[P]Shoit/shoit_network.c
// Function: shoit_network_connect_udp
int shoit_network_connect_udp(struct sockaddr_in *udpServerAddr,int localPort,char *remoteHost,int remoteHostPort)
{
    static struct sockaddr_in udpClientAddr;
    struct hostent *phe;
    int udpSockfd;

    // Fill in the structure whith the address of the server that we want to send to
    // udpServerAddr is class global variable, will be used to send data

    bzero(udpServerAddr, sizeof(struct sockaddr_in));
    udpServerAddr->sin_family = AF_INET;

    if ((phe = gethostbyname(remoteHost)))
        memcpy(&udpServerAddr->sin_addr, phe->h_addr, phe->h_length);
    else if ((udpServerAddr->sin_addr.s_addr = inet_addr(remoteHost)) == INADDR_NONE)
    {   
        perror("can't get host entry");
        return -1;
    }
    udpServerAddr->sin_port = htons(remoteHostPort);

    /* Open a UDP socket */
    if ((udpSockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket error");
        return -1;
    }

    // Allow the port to be reused.
    int yes = 1;
    if (setsockopt(udpSockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
        perror("setsockopt");
        close(udpSockfd);
        return -1;
    } 

    /* Bind any local address for us */
    bzero(&udpClientAddr, sizeof(udpClientAddr));
    udpClientAddr.sin_family = AF_INET;
    udpClientAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    udpClientAddr.sin_port = htons(localPort);
    if((bind(udpSockfd, (struct sockaddr *)&udpClientAddr, sizeof(udpClientAddr))) < 0) {
        perror("UDP client bind error");
        close(udpSockfd);
        return -1;
    }

    return udpSockfd;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x21a0
movq -0x10(%rbp), %rax
movw $0x2, (%rax)
movq -0x20(%rbp), %rdi
callq 0x2240
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x32c4
movq -0x10(%rbp), %rdi
addq $0x4, %rdi
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rsi
movq -0x30(%rbp), %rax
movslq 0x14(%rax), %rdx
callq 0x2280
jmp 0x32f3
movq -0x20(%rbp), %rdi
callq 0x2220
movq -0x10(%rbp), %rcx
movl %eax, 0x4(%rcx)
cmpl $-0x1, %eax
jne 0x32f1
leaq 0x6223(%rip), %rdi      # 0x9503
callq 0x2390
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x33f3
jmp 0x32f3
movl -0x24(%rbp), %eax
movzwl %ax, %edi
callq 0x2100
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x2(%rax)
movl $0x2, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x24c0
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
jge 0x3337
leaq 0x61f2(%rip), %rdi      # 0x9518
callq 0x2390
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x33f3
movl $0x1, -0x38(%rbp)
movl -0x34(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x38(%rbp), %rcx
movl $0x4, %r8d
callq 0x2090
cmpl $-0x1, %eax
jne 0x337c
leaq 0x61bf(%rip), %rdi      # 0x9525
callq 0x2390
movl -0x34(%rbp), %edi
callq 0x21c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x33f3
leaq 0x8ef9(%rip), %rdi      # 0xc27c
xorl %esi, %esi
movl $0x10, %edx
callq 0x21a0
movw $0x2, 0x8ee4(%rip)      # 0xc27c
xorl %edi, %edi
callq 0x2190
movl %eax, 0x8edb(%rip)      # 0xc280
movl -0x14(%rbp), %eax
movzwl %ax, %edi
callq 0x2100
movw %ax, 0x8ec7(%rip)       # 0xc27e
movl -0x34(%rbp), %edi
leaq 0x8ebb(%rip), %rsi      # 0xc27c
movl $0x10, %edx
callq 0x2330
cmpl $0x0, %eax
jge 0x33ed
leaq 0x6159(%rip), %rdi      # 0x9530
callq 0x2390
movl -0x34(%rbp), %edi
callq 0x21c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x33f3
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)