// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x24c0
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
jge 0x3754
leaq 0x5dd5(%rip), %rdi      # 0x9518
callq 0x2390
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x392d
movl $0x1, -0x3c(%rbp)
movl -0x34(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x3c(%rbp), %rcx
movl $0x4, %r8d
callq 0x2090
cmpl $-0x1, %eax
jne 0x3794
leaq 0x5da2(%rip), %rdi      # 0x9525
callq 0x2390
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x392d
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x21a0
movw $0x2, -0x30(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x37c0
movq -0x10(%rbp), %rdi
callq 0x20d0
cmpq $0x0, %rax
jne 0x37cf
xorl %edi, %edi
callq 0x2190
movl %eax, -0x2c(%rbp)
jmp 0x385c
movq -0x10(%rbp), %rdi
callq 0x2240
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
je 0x3804
leaq -0x30(%rbp), %rdi
addq $0x4, %rdi
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rsi
movq -0x48(%rbp), %rax
movslq 0x14(%rax), %rdx
callq 0x2280
jmp 0x385a
movq -0x10(%rbp), %rdi
callq 0x2220
movl %eax, -0x2c(%rbp)
cmpl $-0x1, %eax
jne 0x3858
movq 0x87dc(%rip), %rax      # 0xbff8
movq (%rax), %rax
movq %rax, -0x68(%rbp)
callq 0x2060
movl (%rax), %edi
callq 0x2450
movq -0x68(%rbp), %rdi
movq %rax, %rdx
leaq 0x5d47(%rip), %rsi      # 0x9584
movb $0x0, %al
callq 0x2250
movl -0x34(%rbp), %edi
callq 0x21c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x392d
jmp 0x385a
jmp 0x385c
movl -0x14(%rbp), %eax
movzwl %ax, %edi
callq 0x2100
movw %ax, -0x2e(%rbp)
movl -0x34(%rbp), %edi
leaq -0x30(%rbp), %rsi
movl $0x10, %edx
callq 0x2330
cmpl $0x0, %eax
jge 0x38a1
leaq 0x5cc2(%rip), %rdi      # 0x954a
callq 0x2390
movl -0x34(%rbp), %edi
callq 0x21c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x392d
movl -0x34(%rbp), %edi
movl $0x5, %esi
callq 0x2300
cmpl $0x0, %eax
jge 0x38d0
leaq 0x5ce3(%rip), %rdi      # 0x959d
callq 0x2390
movl -0x34(%rbp), %edi
callq 0x21c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x392d
movl $0x10, -0x5c(%rbp)
movl -0x34(%rbp), %edi
leaq -0x58(%rbp), %rsi
leaq -0x5c(%rbp), %rdx
callq 0x23d0
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jge 0x3905
leaq 0x5cb3(%rip), %rdi      # 0x95aa
callq 0x2390
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x392d
movl -0x34(%rbp), %edi
callq 0x21c0
cmpq $0x0, -0x20(%rbp)
je 0x3927
movq -0x20(%rbp), %rax
movq -0x58(%rbp), %rcx
movq %rcx, (%rax)
movq -0x50(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)