#include <stdio.h>
#include <stdlib.h>

// TODO: Implement a helper named check_permissions that matches the prototype below.
int check_permissions(char *check_against, char *to_check)
{
    for (int i = 0; i < 9; i++)
    {
        if (check_against[i] != '-' && check_against[i] != to_check[i])
        {
            return 0;
        }
    }
    return 1;
}

int main(int argc, char **argv)
{
    if (!(argc == 2 || argc == 3))
    {
        fprintf(stderr, "USAGE: count_large size [permissions]\n");
        return 1;
    }
    // Variables for while loop
    int count = 0;
    int matches = 1;
    int check_flag = 0;
    // Variables for scanf()
    char directory_flag;
    char permissions[9];
    int file_size;
    // TODO: Process command line arguments.
    int threshold = atoi(argv[1]);
    if (argc == 3)
    {
        check_flag = 1;
    }
    // TODO: Call check_permissions as part of your solution to count the files to
    // compute and print the correct value.
    scanf("%*[^\n]");
    while (scanf("%c%s %*s %*s %*s %d %*s %*s %*s %*s", &directory_flag, permissions, &file_size) == 3)
    {
        if (file_size > threshold && directory_flag == '-')
        {
            if (check_flag)
            {
                matches = check_permissions(argv[2], permissions);
            }
            if (matches)
            {
                count++;
            }
        }
    }
    printf("%d\n", count);
    return 0;
}
