// File: /workspace/github_c_makefile_star2_O0/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
subq $0x80, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0x13f5
leaq 0x3c1e(%rip), %rdi      # 0x5004
callq 0x1130
movl $0x1, %edi
callq 0x11b0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1250
movl %eax, -0x14(%rbp)
leaq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10e0
movw $0x2, -0x28(%rbp)
movl -0x14(%rbp), %eax
movzwl %ax, %edi
callq 0x1050
movw %ax, -0x26(%rbp)
xorl %edi, %edi
callq 0x1120
movl %eax, -0x24(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1090
movl %eax, 0x5d2d(%rip)      # 0x7178
cmpl $-0x1, %eax
jne 0x1466
leaq 0x3c2e(%rip), %rdi      # 0x5085
callq 0x1130
movl $0x1, %edi
callq 0x11b0
movl 0x5d0c(%rip), %edi      # 0x7178
leaq -0x28(%rbp), %rsi
movl $0x10, %edx
callq 0x1060
cmpl $-0x1, %eax
jne 0x1495
leaq 0x3c28(%rip), %rdi      # 0x50ae
callq 0x1130
movl $0x1, %edi
callq 0x11b0
movl 0x5cdd(%rip), %edi      # 0x7178
movl $0xa, %esi
callq 0x1170
cmpl $0x0, %eax
jge 0x14c0
leaq 0x3c24(%rip), %rdi      # 0x50d5
callq 0x1130
movl $0x1, %edi
callq 0x11b0
movl $0x2, %edi
leaq 0x134(%rip), %rsi       # 0x1600
callq 0x1190
movq $-0x1, %rcx
cmpq %rcx, %rax
jne 0x14f3
leaq 0x3c17(%rip), %rdi      # 0x50fb
callq 0x1130
movl $0x1, %edi
callq 0x11b0
leaq -0x60(%rbp), %rdi
callq 0x11c0
cmpl $0x0, %eax
je 0x1517
leaq 0x3bfa(%rip), %rdi      # 0x5102
callq 0x1130
movl $0x1, %edi
callq 0x11b0
leaq -0x60(%rbp), %rdi
movl $0x1, %esi
callq 0x11d0
cmpl $0x0, %eax
je 0x1540
leaq 0x3c02(%rip), %rdi      # 0x5133
callq 0x1130
movl $0x1, %edi
callq 0x11b0
jmp 0x1542
movl $0x14, %edi
callq 0x11e0
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x1565
leaq 0x3c02(%rip), %rdi      # 0x5160
callq 0x1130
jmp 0x1542
movl $0x10, -0x74(%rbp)
movl 0x5c06(%rip), %edi      # 0x7178
movq -0x68(%rbp), %rsi
addq $0x4, %rsi
leaq -0x74(%rbp), %rdx
callq 0x10a0
movl %eax, -0x18(%rbp)
cmpl $-0x1, -0x18(%rbp)
jne 0x15a3
leaq 0x3c07(%rip), %rdi      # 0x519a
callq 0x1130
movq -0x68(%rbp), %rdi
callq 0x11a0
jmp 0x1542
leaq 0x3c22(%rip), %rdi      # 0x51cc
movb $0x0, %al
callq 0x1040
movl -0x18(%rbp), %ecx
movq -0x68(%rbp), %rax
movl %ecx, (%rax)
movq -0x68(%rbp), %rcx
leaq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rsi
leaq 0x63(%rip), %rdx        # 0x1630
callq 0x12a0
cmpl $0x0, %eax
je 0x15f1
leaq 0x3c19(%rip), %rdi      # 0x51f7
callq 0x1130
movq -0x68(%rbp), %rdi
callq 0x11a0
jmp 0x1542
jmp 0x1542
nopw %cs:(%rax,%rax)