// File: /workspace/github_c_makefile_star2_O2/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 0x1516
movq 0x8(%rsi), %rdi
callq 0x1250
andq $0x0, -0x34(%rbp)
movw $0x2, -0x3c(%rbp)
rolw $0x8, %ax
movw %ax, -0x3a(%rbp)
andl $0x0, -0x38(%rbp)
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x1080
movl %eax, 0x4d6f(%rip)      # 0x6178
cmpl $-0x1, %eax
je 0x151f
leaq -0x3c(%rbp), %rsi
pushq $0x10
popq %rdx
movl %eax, %edi
callq 0x1050
cmpl $-0x1, %eax
je 0x1528
movl 0x4d49(%rip), %edi      # 0x6178
pushq $0xa
popq %rsi
callq 0x1160
testl %eax, %eax
js 0x1531
leaq 0x11a(%rip), %rsi       # 0x1560
pushq $0x2
popq %rdi
callq 0x1180
cmpq $-0x1, %rax
je 0x153a
leaq -0x80(%rbp), %rdi
callq 0x11b0
testl %eax, %eax
jne 0x1543
leaq -0x80(%rbp), %rdi
pushq $0x1
popq %rsi
callq 0x11c0
testl %eax, %eax
jne 0x154c
pushq $0x14
popq %rbx
leaq 0x2d39(%rip), %r14      # 0x41c0
leaq -0x2c(%rbp), %r15
movq %rbx, %rdi
callq 0x11d0
testq %rax, %rax
jne 0x14a2
movq %r14, %rdi
callq 0x1120
jmp 0x148b
movq %rax, %r13
movl $0x10, -0x2c(%rbp)
movl 0x4cc6(%rip), %edi      # 0x6178
movq %rax, %rsi
addq $0x4, %rsi
movq %r15, %rdx
callq 0x1090
cmpl $-0x1, %eax
jne 0x14cf
leaq 0x2d2d(%rip), %rdi      # 0x41fa
jmp 0x1504
movl %eax, %r12d
leaq 0x3029(%rip), %rdi      # 0x4502
callq 0x1230
movl %r12d, (%r13)
leaq -0x48(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq 0x91(%rip), %rdx        # 0x1582
movq %r13, %rcx
callq 0x12a0
testl %eax, %eax
je 0x148b
leaq 0x2d28(%rip), %rdi      # 0x422c
callq 0x1120
movq %r13, %rdi
callq 0x1190
jmp 0x148b
leaq 0x2b47(%rip), %rdi      # 0x4064
jmp 0x1553
leaq 0x2bbf(%rip), %rdi      # 0x40e5
jmp 0x1553
leaq 0x2bdf(%rip), %rdi      # 0x410e
jmp 0x1553
leaq 0x2bfd(%rip), %rdi      # 0x4135
jmp 0x1553
leaq 0x2c1a(%rip), %rdi      # 0x415b
jmp 0x1553
leaq 0x2c18(%rip), %rdi      # 0x4162
jmp 0x1553
leaq 0x2c40(%rip), %rdi      # 0x4193
callq 0x1120
pushq $0x1
popq %rdi
callq 0x11a0