// File: /workspace/github_c_makefile_star2_O2/pencode-lab[P]Shoit/shoit_network.c
// Function: shoit_network_tcp_server_init
int shoit_network_tcp_server_init(char *serverHost,int serverPort,struct sockaddr_in *clientAddr)
{
    struct sockaddr_in tcpServerAddr;
    int listenfd,socketfd;

    // Create TCP connection as a server in order to transmit the error list 
    // Open a TCP socket 
    if((listenfd = socket(AF_INET, SOCK_STREAM, 0))<0) {
        perror("socket error");
        return(-1);
    }

    int yes=1;
    if (setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
        perror("setsockopt");
        return(-1);
    } 
    
    /* Bind our local address so that the client can send to us */
    bzero(&tcpServerAddr, sizeof(tcpServerAddr));
    tcpServerAddr.sin_family = AF_INET;

    if(serverHost==NULL || strlen(serverHost)==0){                                                                                                    
        tcpServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);                                                                           
    }else{                                                                                                                           
        struct hostent *phe;                                                                                                         
        if ((phe = gethostbyname(serverHost)))                                                                                 
            memcpy(&tcpServerAddr.sin_addr, phe->h_addr, phe->h_length);                                                             
        else if ((tcpServerAddr.sin_addr.s_addr = inet_addr(serverHost)) == INADDR_NONE){                                    
            fprintf(stderr,"bind localhost error:%s\n",strerror(errno));                                                             
            close(listenfd);
            return(-1);
        }                                                                                                                            
                                                                                                                                     
    }                                                                                                                                
    tcpServerAddr.sin_port = htons(serverPort);                                                                                

    if((bind(listenfd, (struct sockaddr *)&tcpServerAddr, sizeof(tcpServerAddr))) < 0) {
        perror("bind error");
        close(listenfd);
        return(-1);
    }
    
    if((listen(listenfd, 5))<0) {
        perror("listen error");
        close(listenfd);
        return(-1);
    }

   
    struct sockaddr_in tcpClientAddr;
    socklen_t clilen;

    clilen = sizeof(tcpClientAddr);
    socketfd = accept(listenfd, (struct sockaddr *) &tcpClientAddr, &clilen);

    if (socketfd < 0)
    {
        perror("accept error");
        return -1;
    } 

    close(listenfd);

    if(clientAddr!=NULL){
        memcpy(clientAddr,&tcpClientAddr,sizeof(tcpClientAddr));
    }


    return socketfd;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %r15
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x24c0
testl %eax, %eax
js 0x318e
movl %eax, %ebx
leaq 0x14(%rsp), %rcx
movl $0x1, (%rcx)
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
pushq $0x4
popq %r8
movl %eax, %edi
callq 0x2090
cmpl $-0x1, %eax
je 0x3197
andl $0x0, 0xc(%rsp)
andq $0x0, 0x4(%rsp)
movw $0x2, (%rsp)
testq %r15, %r15
je 0x3133
cmpb $0x0, (%r15)
je 0x3133
movq %r15, %rdi
callq 0x2240
testq %rax, %rax
je 0x311e
leaq 0x4(%rsp), %rdi
movq 0x18(%rax), %rcx
movq (%rcx), %rsi
movslq 0x14(%rax), %rdx
callq 0x2280
jmp 0x3133
movq %r15, %rdi
callq 0x2220
movl %eax, 0x4(%rsp)
cmpl $-0x1, %eax
je 0x31da
rolw $0x8, %bp
movq %rsp, %rsi
movw %bp, 0x2(%rsi)
pushq $0x10
popq %rdx
movl %ebx, %edi
callq 0x2330
testl %eax, %eax
js 0x31a0
pushq $0x5
popq %rsi
movl %ebx, %edi
callq 0x2300
testl %eax, %eax
js 0x31a9
leaq 0x10(%rsp), %rdx
movl $0x10, (%rdx)
leaq 0x18(%rsp), %rsi
movl %ebx, %edi
callq 0x23d0
testl %eax, %eax
js 0x31be
movl %eax, %ebp
movl %ebx, %edi
callq 0x21b0
testq %r14, %r14
je 0x31cd
movups 0x18(%rsp), %xmm0
movups %xmm0, (%r14)
jmp 0x31cd
leaq 0x32b8(%rip), %rdi      # 0x644d
jmp 0x31c5
leaq 0x32bc(%rip), %rdi      # 0x645a
jmp 0x31c5
leaq 0x32d8(%rip), %rdi      # 0x647f
jmp 0x31b0
leaq 0x3322(%rip), %rdi      # 0x64d2
callq 0x2390
movl %ebx, %edi
callq 0x21b0
jmp 0x31ca
leaq 0x331a(%rip), %rdi      # 0x64df
callq 0x2390
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x5e17(%rip), %rax      # 0x8ff8
movq (%rax), %r14
callq 0x2060
movl (%rax), %edi
callq 0x2450
leaq 0x32c2(%rip), %rsi      # 0x64b9
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2250
jmp 0x31b5