// File: /workspace/github_cpp_makefile_star2_O1/XuhuiSun95[P]Ticket-Booking-Service/server/ServerSocket.cpp
// Function: ServerSocket::Init(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, int const&, bool)
void ServerSocket::Init(const std::string &ipaddress, const int &port, bool host) {

    if((mSockfd = socket(AF_INET, SOCK_STREAM, 0))==0) {
        std::cout << "Socket creation error" << std::endl;
        return;
    }
    
    //setup socket address structure
    memset(&mAddress, '0', sizeof(mAddress));
    mAddress.sin_family = AF_INET;
    mAddress.sin_port = htons(port);

    if(host) {

        mAddress.sin_addr.s_addr = INADDR_ANY;

        // call bind to associate the socket with our local address and port
        if(bind(mSockfd, (struct sockaddr *)&mAddress, sizeof(mAddress))<0) {
            std::cout << "Bind Failed" << std::endl;
            return;
        }

        // convert the socket to listen for incoming connections
        if(listen(mSockfd, 10)<0) {
            std::cout << "Listen Failed" << std::endl;
            return;
        }
    }
    else {

        if(inet_pton(AF_INET, ipaddress.c_str(), &mAddress.sin_addr)<=0) {
            std::cout << "Invalid pair server address/ Address not supported" << std::endl;
            return;
        }

        if(connect(mSockfd, (struct sockaddr *)&mAddress, sizeof(mAddress))<0) {
            std::cout << "Connection Failed" << std::endl;
            return;
        }
    }

    mValid = true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %ecx, %r12d
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %rbx
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2060
movl %eax, 0x4(%rbx)
testl %eax, %eax
je 0x4006
leaq 0xc(%rbx), %r14
movaps 0x1449(%rip), %xmm0     # 0x53f0
movups %xmm0, 0xc(%rbx)
movw $0x2, 0xc(%rbx)
movzwl (%r13), %ecx
rolw $0x8, %cx
movw %cx, 0xe(%rbx)
testb %r12b, %r12b
je 0x401b
movl $0x0, 0x10(%rbx)
movl %eax, %edi
movq %r14, %rsi
movl $0x10, %edx
callq 0x2050
testl %eax, %eax
js 0x4056
movl 0x4(%rbx), %edi
movl $0xa, %esi
callq 0x2200
testl %eax, %eax
jns 0x4044
movq 0x2fdb(%rip), %rbx      # 0x6fd0
leaq 0x1426(%rip), %rsi      # 0x5422
movl $0xd, %edx
jmp 0x4093
movq 0x2fc3(%rip), %rbx      # 0x6fd0
leaq 0x13ec(%rip), %rsi      # 0x5400
movl $0x15, %edx
jmp 0x4093
movq (%r15), %rsi
leaq 0x10(%rbx), %rdx
movl $0x2, %edi
callq 0x2310
testl %eax, %eax
jle 0x406b
movl 0x4(%rbx), %edi
movq %r14, %rsi
movl $0x10, %edx
callq 0x2190
testl %eax, %eax
js 0x4080
movb $0x1, (%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2f73(%rip), %rbx      # 0x6fd0
leaq 0x13b2(%rip), %rsi      # 0x5416
movl $0xb, %edx
jmp 0x4093
movq 0x2f5e(%rip), %rbx      # 0x6fd0
leaq 0x13b7(%rip), %rsi      # 0x5430
movl $0x32, %edx
jmp 0x4093
movq 0x2f49(%rip), %rbx      # 0x6fd0
leaq 0x13d5(%rip), %rsi      # 0x5463
movl $0x11, %edx
movq %rbx, %rdi
callq 0x2210
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2150