// File: /workspace/github_c_makefile_star2_O0/pencode-lab[P]Shoit/shoit_network.c
// Function: shoit_network_udp_server_init
int shoit_network_udp_server_init(struct sockaddr_in *udpLocalAddr,int udpLocalPort,char *remoteHost,int remoteUdpPort)
{
    struct sockaddr_in cliaddr;
    struct hostent *phe;
    int udpSockfd;
    

    // Create a UDP sink 
    if ((udpSockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
        perror("socket error");
        return udpSockfd;
    }
    bzero((char *)udpLocalAddr, sizeof(struct sockaddr_in));
    udpLocalAddr->sin_family = AF_INET;
    udpLocalAddr->sin_addr.s_addr = htonl(INADDR_ANY);
    udpLocalAddr->sin_port = htons(udpLocalPort);

    if((bind(udpSockfd, (struct sockaddr *)udpLocalAddr, sizeof(struct sockaddr_in))) < 0) {
        perror("UDP bind error");
        close(udpSockfd);
        return -1;
    }

    // Use connected UDP to receive only from a specific host and port.
    bzero(&cliaddr, sizeof(cliaddr));
    if ((phe = gethostbyname(remoteHost)))
        memcpy(&cliaddr.sin_addr, phe->h_addr, phe->h_length);
    else if ((cliaddr.sin_addr.s_addr = inet_addr(remoteHost)) == INADDR_NONE){
        perror("gethostbyname remotehost error");
        close(udpSockfd);
        return -1;
    }

    cliaddr.sin_family = AF_INET;
    cliaddr.sin_port = htons(remoteUdpPort);

    if (connect(udpSockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr)) < 0)
    {
        perror("connect() error");
        close(udpSockfd);
        return -1;
    }

    return udpSockfd;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl $0x2, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x24c0
movl %eax, -0x44(%rbp)
cmpl $0x0, %eax
jge 0x3443
leaq 0x60e5(%rip), %rdi      # 0x9518
callq 0x2390
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x357b
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x21a0
movq -0x10(%rbp), %rax
movw $0x2, (%rax)
xorl %edi, %edi
callq 0x2190
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x14(%rbp), %eax
movzwl %ax, %edi
callq 0x2100
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x2(%rax)
movl -0x44(%rbp), %edi
movq -0x10(%rbp), %rsi
movl $0x10, %edx
callq 0x2330
cmpl $0x0, %eax
jge 0x34b8
leaq 0x60a7(%rip), %rdi      # 0x9546
callq 0x2390
movl -0x44(%rbp), %edi
callq 0x21c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x357b
leaq -0x34(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x21a0
movq -0x20(%rbp), %rdi
callq 0x2240
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x34fd
leaq -0x34(%rbp), %rdi
addq $0x4, %rdi
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rsi
movq -0x40(%rbp), %rax
movslq 0x14(%rax), %rdx
callq 0x2280
jmp 0x352d
movq -0x20(%rbp), %rdi
callq 0x2220
movl %eax, -0x30(%rbp)
cmpl $-0x1, %eax
jne 0x352b
leaq 0x6040(%rip), %rdi      # 0x9555
callq 0x2390
movl -0x44(%rbp), %edi
callq 0x21c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x357b
jmp 0x352d
movw $0x2, -0x34(%rbp)
movl -0x24(%rbp), %eax
movzwl %ax, %edi
callq 0x2100
movw %ax, -0x32(%rbp)
movl -0x44(%rbp), %edi
leaq -0x34(%rbp), %rsi
movl $0x10, %edx
callq 0x2400
cmpl $0x0, %eax
jge 0x3575
leaq 0x6015(%rip), %rdi      # 0x9574
callq 0x2390
movl -0x44(%rbp), %edi
callq 0x21c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x357b
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)