// File: /workspace/github_c_makefile_star2_O3/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
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x24c0
testl %eax, %eax
js 0x3408
movl %eax, %ebx
leaq 0x14(%rsp), %rcx
movl $0x1, (%rcx)
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x2090
cmpl $-0x1, %eax
je 0x3411
movl $0x0, 0xc(%rsp)
movq $0x0, 0x4(%rsp)
movw $0x2, (%rsp)
testq %r15, %r15
je 0x33a9
cmpb $0x0, (%r15)
je 0x33a9
movq %r15, %rdi
callq 0x2240
testq %rax, %rax
je 0x3394
leaq 0x4(%rsp), %rdi
movq 0x18(%rax), %rcx
movq (%rcx), %rsi
movslq 0x14(%rax), %rdx
callq 0x2280
jmp 0x33a9
movq %r15, %rdi
callq 0x2220
movl %eax, 0x4(%rsp)
cmpl $-0x1, %eax
je 0x3456
rolw $0x8, %bp
movq %rsp, %rsi
movw %bp, 0x2(%rsi)
movl %ebx, %edi
movl $0x10, %edx
callq 0x2330
testl %eax, %eax
js 0x341a
movl %ebx, %edi
movl $0x5, %esi
callq 0x2300
testl %eax, %eax
js 0x3423
leaq 0x10(%rsp), %rdx
movl $0x10, (%rdx)
leaq 0x18(%rsp), %rsi
movl %ebx, %edi
callq 0x23d0
testl %eax, %eax
js 0x3438
movl %eax, %ebp
movl %ebx, %edi
callq 0x21b0
testq %r14, %r14
je 0x3449
movups 0x18(%rsp), %xmm0
movups %xmm0, (%r14)
jmp 0x3449
leaq 0x303e(%rip), %rdi      # 0x644d
jmp 0x343f
leaq 0x3042(%rip), %rdi      # 0x645a
jmp 0x343f
leaq 0x305e(%rip), %rdi      # 0x647f
jmp 0x342a
leaq 0x30a8(%rip), %rdi      # 0x64d2
callq 0x2390
movl %ebx, %edi
callq 0x21b0
jmp 0x3444
leaq 0x30a0(%rip), %rdi      # 0x64df
callq 0x2390
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl %ebx, %edi
callq 0x25ca
jmp 0x3444