// File: /workspace/github_c_makefile_star2_O1/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 0x3401
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 0x340a
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movw $0x2, (%rsp)
testq %r15, %r15
je 0x3380
cmpb $0x0, (%r15)
je 0x3380
movq %r15, %rdi
callq 0x2240
testq %rax, %rax
je 0x33e7
leaq 0x4(%rsp), %rdi
movq 0x18(%rax), %rcx
movq (%rcx), %rsi
movslq 0x14(%rax), %rdx
callq 0x2280
jmp 0x3388
movl $0x0, 0x4(%rsp)
rolw $0x8, %bp
movq %rsp, %rsi
movw %bp, 0x2(%rsi)
movl %ebx, %edi
movl $0x10, %edx
callq 0x2330
testl %eax, %eax
js 0x3413
movl %ebx, %edi
movl $0x5, %esi
callq 0x2300
testl %eax, %eax
js 0x341c
leaq 0x10(%rsp), %rdx
movl $0x10, (%rdx)
leaq 0x18(%rsp), %rsi
movl %ebx, %edi
callq 0x23d0
testl %eax, %eax
js 0x3431
movl %eax, %ebp
movl %ebx, %edi
callq 0x21b0
testq %r14, %r14
je 0x3442
movups 0x18(%rsp), %xmm0
movups %xmm0, (%r14)
jmp 0x3442
movq %r15, %rdi
callq 0x2220
movl %eax, 0x4(%rsp)
cmpl $-0x1, %eax
jne 0x3388
movl %ebx, %edi
callq 0x25ca
jmp 0x343d
leaq 0x3064(%rip), %rdi      # 0x646c
jmp 0x3438
leaq 0x3068(%rip), %rdi      # 0x6479
jmp 0x3438
leaq 0x3084(%rip), %rdi      # 0x649e
jmp 0x3423
leaq 0x30ce(%rip), %rdi      # 0x64f1
callq 0x2390
movl %ebx, %edi
callq 0x21b0
jmp 0x343d
leaq 0x30c6(%rip), %rdi      # 0x64fe
callq 0x2390
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq