// File: /workspace/github_c_makefile_star2_O3/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 0x153a
movq 0x8(%rsi), %rdi
callq 0x1260
movq $0x0, -0x34(%rbp)
movw $0x2, -0x3c(%rbp)
rolw $0x8, %ax
movw %ax, -0x3a(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1080
movl %eax, 0x4d5d(%rip)      # 0x6180
cmpl $-0x1, %eax
je 0x1543
leaq -0x3c(%rbp), %rsi
movl %eax, %edi
movl $0x10, %edx
callq 0x1050
cmpl $-0x1, %eax
je 0x154c
movl 0x4d35(%rip), %edi      # 0x6180
movl $0xa, %esi
callq 0x1170
testl %eax, %eax
js 0x1555
leaq 0x122(%rip), %rsi       # 0x1586
movl $0x2, %edi
callq 0x1190
cmpq $-0x1, %rax
je 0x155e
leaq -0x80(%rbp), %rdi
callq 0x11c0
testl %eax, %eax
jne 0x1567
leaq -0x80(%rbp), %rdi
movl $0x1, %esi
callq 0x11d0
testl %eax, %eax
jne 0x1570
leaq 0x2d1a(%rip), %rbx      # 0x41c0
leaq -0x2c(%rbp), %r14
leaq -0x80(%rbp), %r12
movl $0x14, %edi
callq 0x11e0
testq %rax, %rax
jne 0x14c7
movq %rbx, %rdi
callq 0x1130
jmp 0x14ae
movq %rax, %r13
movl $0x10, -0x2c(%rbp)
movl 0x4ca9(%rip), %edi      # 0x6180
movq %rax, %rsi
addq $0x4, %rsi
movq %r14, %rdx
callq 0x1090
cmpl $-0x1, %eax
jne 0x14f4
leaq 0x2d08(%rip), %rdi      # 0x41fa
jmp 0x1528
movl %eax, %r15d
leaq 0x3004(%rip), %rdi      # 0x4502
callq 0x1240
movl %r15d, (%r13)
leaq -0x48(%rbp), %rdi
movq %r12, %rsi
leaq 0x93(%rip), %rdx        # 0x15a8
movq %r13, %rcx
callq 0x12b0
testl %eax, %eax
je 0x14ae
leaq 0x2d04(%rip), %rdi      # 0x422c
callq 0x1130
movq %r13, %rdi
callq 0x11a0
jmp 0x14ae
leaq 0x2b23(%rip), %rdi      # 0x4064
jmp 0x1577
leaq 0x2b9b(%rip), %rdi      # 0x40e5
jmp 0x1577
leaq 0x2bbb(%rip), %rdi      # 0x410e
jmp 0x1577
leaq 0x2bd9(%rip), %rdi      # 0x4135
jmp 0x1577
leaq 0x2bf6(%rip), %rdi      # 0x415b
jmp 0x1577
leaq 0x2bf4(%rip), %rdi      # 0x4162
jmp 0x1577
leaq 0x2c1c(%rip), %rdi      # 0x4193
callq 0x1130
movl $0x1, %edi
callq 0x11b0