// File: Lartu[P]coraldb/source/coraldb.cpp
// Function: start_socket()
void start_socket()
{
    // Create socket
    int sockfd;
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        err_socket_creation(strerror(errno));
        exit(1);
    }
    // Bind Socket
    struct sockaddr_in address;
    memset(&address, 0, sizeof(address));
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(global_port);
    socklen_t len = sizeof(address);
    if (bind(sockfd, (struct sockaddr *)&address, len) < 0)
    {
        err_socket_creation(strerror(errno));
        exit(1);
    }
    getsockname(sockfd, (struct sockaddr *)&address, &len);
    global_port = ntohs(address.sin_port);
    // Inform the user
    std::cout << "CoralDB running on port " << global_port << ", backed to " << global_filename << std::endl;
    // Listen socket
    int new_socket_fd;
    if (listen(sockfd, 256) < 0)
    {
        err_socket_read(strerror(errno));
    }
    while (true)
    {
        if ((new_socket_fd = accept(sockfd, (struct sockaddr *)&address, &len)) < 0)
        {
            err_socket_accept(strerror(errno));
        }
        std::thread child_thread(handle_socket, new_socket_fd);
        child_thread.detach();
    }
    close(sockfd);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x30b0
movl %eax, -0x4(%rbp)
cmpl $0x0, %eax
jge 0x5c2f
callq 0x3040
movl (%rax), %edi
callq 0x3130
movq %rax, %rdi
callq 0x3960
movl $0x1, %edi
callq 0x3370
leaq -0x14(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x31f0
movw $0x2, -0x14(%rbp)
movl $0x0, -0x10(%rbp)
movl 0xb692(%rip), %eax      # 0x112e4
movzwl %ax, %edi
callq 0x3050
movw %ax, -0x12(%rbp)
movl $0x10, -0x18(%rbp)
movl -0x4(%rbp), %edi
movl -0x18(%rbp), %edx
leaq -0x14(%rbp), %rsi
callq 0x3060
cmpl $0x0, %eax
jge 0x5c97
callq 0x3040
movl (%rax), %edi
callq 0x3130
movq %rax, %rdi
callq 0x3960
movl $0x1, %edi
callq 0x3370
movl -0x4(%rbp), %edi
leaq -0x14(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x32d0
movzwl -0x12(%rbp), %edi
callq 0x3520
movzwl %ax, %eax
movl %eax, 0xb62b(%rip)      # 0x112e4
movq 0xb308(%rip), %rdi      # 0x10fc8
leaq 0x57ae(%rip), %rsi      # 0xb475
callq 0x3300
movq %rax, %rdi
movl 0xb60f(%rip), %esi      # 0x112e4
callq 0x33f0
movq %rax, %rdi
leaq 0x57aa(%rip), %rsi      # 0xb48e
callq 0x3300
movq %rax, %rdi
leaq 0xb6bd(%rip), %rsi      # 0x113b0
callq 0x32a0
movq %rax, %rdi
movq 0xb2a6(%rip), %rsi      # 0x10fa8
callq 0x3320
movl -0x4(%rbp), %edi
movl $0x100, %esi            # imm = 0x100
callq 0x3340
cmpl $0x0, %eax
jge 0x5d2d
callq 0x3040
movl (%rax), %edi
callq 0x3130
movq %rax, %rdi
callq 0x39b0
jmp 0x5d2f
movl -0x4(%rbp), %edi
leaq -0x14(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x3110
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jge 0x5d5b
callq 0x3040
movl (%rax), %edi
callq 0x3130
movq %rax, %rdi
callq 0x3a00
leaq -0x422(%rip), %rsi      # 0x5940
leaq -0x28(%rbp), %rdi
movq %rdi, -0x40(%rbp)
leaq -0x1c(%rbp), %rdx
callq 0x6760
movq -0x40(%rbp), %rdi
callq 0x34f0
jmp 0x5d7e
leaq -0x28(%rbp), %rdi
callq 0x6820
jmp 0x5d2f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x6820
movq -0x30(%rbp), %rdi
callq 0x3500
nopw (%rax,%rax)