// File: /workspace/github_c_makefile_star2_O1/evanwire[P]GitClone/server.c
// Function: main
int main(int argc, char *argv[]) {

    //Declare variables
    int port_number, connection_fd;
    struct sockaddr_in address;
    pthread_attr_t pthread_attribute;
    pthread_arg_t *pthread_arguments;
    pthread_t pthread;
    socklen_t client_address_size;

    //Get server port
    if(argc != 2){
        perror("\nError: Incorrect amount of command line parameters. Please only specify this file's executable and a port number to listen on.\n");
        exit(1);
    }

    port_number = atoi(argv[1]);

    //Initialize server address
    memset(&address, 0, sizeof address);
    address.sin_family = AF_INET;
    address.sin_port = htons(port_number);
    address.sin_addr.s_addr = htonl(INADDR_ANY);

    //Create server socket
    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("\nError: could not create server socket.\n");
        exit(1);
    }

    //Bind server to socket
    if (bind(server_fd, (struct sockaddr *)&address, sizeof address) == -1) {
        perror("\nError: could not bind server socket.\n");
        exit(1);
    }

    //Begin listening on socket
    if (listen(server_fd, 10) < 0) {
        perror("\nError: server was unable to listen.\n");
        exit(1);
    }

    //Signal handler to handle closing the server with ctrl-c
    if (signal(SIGINT, signal_handler) == SIG_ERR) {
        perror("signal");
        exit(1);
    }

    //Initialize pthread attribute
    if (pthread_attr_init(&pthread_attribute) != 0) {
        perror("\nError: could not initialize pthread attribute.\n");
        exit(1);
    }
    if (pthread_attr_setdetachstate(&pthread_attribute, PTHREAD_CREATE_DETACHED) != 0) {
        perror("\nError: could not create detatched threads.\n");
        exit(1);
    }

    while (1) {
        //Create pthread instance for each client
        pthread_arguments = (pthread_arg_t *)malloc(sizeof *pthread_arguments);
        if (!pthread_arguments) {
            perror("\nError: Could not malloc for the server thread arguments\n");
            continue;
        }

        //Accept connection to client
        client_address_size = sizeof pthread_arguments->client_address;
        connection_fd = accept(server_fd, (struct sockaddr *)&pthread_arguments->client_address, &client_address_size);
        if (connection_fd == -1) {
            perror("\nError: could not accept connection from client.\n");
            free(pthread_arguments);
            continue;
        }

        printf("\nSTATUS: Accepted connection from client.\n");

        //Initialize pthread argument for the connection file descriptor
        pthread_arguments->connection_fd = connection_fd;

        //Create the thread to handle the client
        if (pthread_create(&pthread, &pthread_attribute, pthread_function, (void *)pthread_arguments) != 0) {
            perror("\nError: could not create a thread to handle the client.\n");
            free(pthread_arguments);
            continue;
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
cmpl $0x2, %edi
jne 0x1529
movq 0x8(%rsi), %rdi
callq 0x1250
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movw $0x2, -0x40(%rbp)
rolw $0x8, %ax
movw %ax, -0x3e(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1080
movl %eax, 0x4d66(%rip)      # 0x6178
cmpl $-0x1, %eax
je 0x1532
leaq -0x40(%rbp), %rsi
movl %eax, %edi
movl $0x10, %edx
callq 0x1050
cmpl $-0x1, %eax
je 0x153b
movl 0x4d3e(%rip), %edi      # 0x6178
movl $0xa, %esi
callq 0x1160
testl %eax, %eax
js 0x1544
leaq 0x122(%rip), %rsi       # 0x1575
movl $0x2, %edi
callq 0x1180
cmpq $-0x1, %rax
je 0x154d
leaq -0x80(%rbp), %rdi
callq 0x11b0
testl %eax, %eax
jne 0x1556
leaq -0x80(%rbp), %rdi
movl $0x1, %esi
callq 0x11c0
testl %eax, %eax
jne 0x155f
leaq 0x2d2b(%rip), %rbx      # 0x41c0
leaq -0x2c(%rbp), %r14
leaq -0x80(%rbp), %r12
movl $0x14, %edi
callq 0x11d0
testq %rax, %rax
jne 0x14b6
movq %rbx, %rdi
callq 0x1120
jmp 0x149d
movq %rax, %r13
movl $0x10, -0x2c(%rbp)
movl 0x4cb2(%rip), %edi      # 0x6178
movq %rax, %rsi
addq $0x4, %rsi
movq %r14, %rdx
callq 0x1090
cmpl $-0x1, %eax
jne 0x14e3
leaq 0x2d19(%rip), %rdi      # 0x41fa
jmp 0x1517
movl %eax, %r15d
leaq 0x3018(%rip), %rdi      # 0x4505
callq 0x1230
movl %r15d, (%r13)
leaq -0x48(%rbp), %rdi
movq %r12, %rsi
leaq 0x93(%rip), %rdx        # 0x1597
movq %r13, %rcx
callq 0x12a0
testl %eax, %eax
je 0x149d
leaq 0x2d15(%rip), %rdi      # 0x422c
callq 0x1120
movq %r13, %rdi
callq 0x1190
jmp 0x149d
leaq 0x2b34(%rip), %rdi      # 0x4064
jmp 0x1566
leaq 0x2bac(%rip), %rdi      # 0x40e5
jmp 0x1566
leaq 0x2bcc(%rip), %rdi      # 0x410e
jmp 0x1566
leaq 0x2bea(%rip), %rdi      # 0x4135
jmp 0x1566
leaq 0x2c07(%rip), %rdi      # 0x415b
jmp 0x1566
leaq 0x2c05(%rip), %rdi      # 0x4162
jmp 0x1566
leaq 0x2c2d(%rip), %rdi      # 0x4193
callq 0x1120
movl $0x1, %edi
callq 0x11a0