// File: /workspace/github_c_makefile_star2_O1/justy989[P]ce/ce_commands.c
// Function: command_discover_directory_files
CeCommandStatus_t command_discover_directory_files(CeCommand_t* command, void* user_data){
    CeApp_t* app = user_data;
    CommandContext_t command_context = {};

    if(!get_command_context(app, &command_context)) return CE_COMMAND_NO_ACTION;

    if(command->arg_count <= 0 ||
       command->args[0].type != CE_COMMAND_ARG_STRING){
        return CE_COMMAND_PRINT_HELP;
    }

    char base_directory[MAX_PATH_LEN];
#if defined(PLATFORM_WINDOWS)
    snprintf(base_directory, MAX_PATH_LEN, "%s\\*", command->args[0].string);
#else
    snprintf(base_directory, MAX_PATH_LEN, "%s", command->args[0].string);
#endif

    // setup our ignore dirs
    int ignore_dir_count = (command->arg_count - 1);
    char** ignore_dirs = malloc(sizeof(*ignore_dirs) * ignore_dir_count);
    for(int64_t i = 1; i < command->arg_count; i++){
        // TODO: this imposes a restriction that folders cannot be named numbers like 5
        if(command->args[i].type != CE_COMMAND_ARG_STRING){
            return CE_COMMAND_PRINT_HELP;
        }
        ignore_dirs[i - 1] = strdup(command->args[i].string);
    }

    int* ignore_dir_lens = malloc(sizeof(*ignore_dir_lens) * ignore_dir_count);
    for(int64_t i = 0; i < ignore_dir_count; i++){
        ignore_dir_lens[i] = strlen(ignore_dirs[i]);
    }

    // head is a dummy node that doesn't contain any info, just makes the find_files_in_directory_recursively()
    // interface better
    CeStrNode_t* head = calloc(sizeof(*head), 1);
    find_files_in_directory_recursively(base_directory, head, ignore_dirs, ignore_dir_lens, ignore_dir_count);

    // cleanup our ignore dirs
    for(int64_t i = 0; i < ignore_dir_count; i++){
        free(ignore_dirs[i]);
    }
    free(ignore_dirs);
    free(ignore_dir_lens);

    // Count the paths.
    int64_t filepath_count = 0;
    CeStrNode_t* itr = head;
    while(itr){
        if(itr->string){
            filepath_count++;
        }
        itr = itr->next;
    }

    // Build an array of strings for the paths.
    char** filepaths = malloc(filepath_count * sizeof(filepaths[0]));
    bool* filepaths_already_exists = calloc(filepath_count, sizeof(filepaths_already_exists[0]));

    int64_t filepath_index = 0;
    itr = head;
    while(itr){
        if(itr->string){
            filepaths[filepath_index] = strdup(itr->string);
            filepath_index++;
        }
        CeStrNode_t* node = itr;
        itr = itr->next;
        free(node->string);
        free(node);
    }

    // Sort the filepaths.
    qsort(filepaths,
          filepath_count,
          sizeof(filepaths[0]),
          _strstrcmp);

    // Since both lists are sorted, compare the elements in lock step.
    int64_t matching_filepath_count = 0;
    int64_t discovered_index = 0;
    filepath_index = 0;
    while(filepath_index < filepath_count &&
          discovered_index < app->discovered_filepath_count){
        int rc = strcmp(filepaths[filepath_index],
                        app->discovered_filepaths[discovered_index]);
        if(rc < 0){
            discovered_index++;
        }else if(rc > 0){
            filepath_index++;
        }else{
            filepaths_already_exists[filepath_index] = true;
            matching_filepath_count++;
            filepath_index++;
            discovered_index++;
        }
    }

    int64_t new_filepath_count = filepath_count - matching_filepath_count;
    if(new_filepath_count > 0){
        int64_t new_discovered_filepath_count = app->discovered_filepath_count + new_filepath_count;
        app->discovered_filepaths = realloc(
            app->discovered_filepaths,
            sizeof(*app->discovered_filepaths) * new_discovered_filepath_count);

        discovered_index = app->discovered_filepath_count;
        for(int64_t i = 0; i < filepath_count; i++){
            if(!filepaths_already_exists[i]){
                app->discovered_filepaths[discovered_index] = strdup(filepaths[i]);
                discovered_index++;
            }
        }

        app->discovered_filepath_count = new_discovered_filepath_count;

        // Sort the final list of paths.
        qsort(app->discovered_filepaths,
              app->discovered_filepath_count,
              sizeof(app->discovered_filepaths[0]),
              _strstrcmp);
    }

    for(int64_t i = 0; i < filepath_count; i++){
        free(filepaths[i]);
    }
    free(filepaths);
    free(filepaths_already_exists);

    // setup input and completion
    ce_app_input(app, "Load Discovered File", load_project_file_input_complete_func);
    ce_complete_init(&app->input_complete, (const char**)(app->discovered_filepaths), NULL, app->discovered_filepath_count);
    build_complete_list(app->complete_list_buffer, &app->input_complete);

    return CE_COMMAND_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movl $0x3, %r14d
cmpq $0x0, 0x27f8(%rsi)
je 0x177f1
movl %r14d, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %r15
movl $0x2, %r14d
cmpq $0x0, 0x208(%rdi)
jle 0x177dc
movq 0x200(%r15), %rax
cmpl $0x2, (%rax)
jne 0x177dc
movq %rsi, %rbx
movq 0x8(%rax), %rcx
leaq 0x1a09e(%rip), %rdx     # 0x318bc
leaq 0x10(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
xorl %eax, %eax
callq 0xd260
movq 0x208(%r15), %r13
movq %r13, %rax
shlq $0x20, %rax
movabsq $-0x100000000, %r12     # imm = 0xFFFFFFFF00000000
addq %rax, %r12
movq %r12, %rdi
sarq $0x1d, %rdi
callq 0xd4c0
movq %rax, %r14
cmpq $0x2, %r13
jl 0x17892
movq 0x200(%r15), %r15
decq %r13
addq $0x18, %r15
xorl %ebp, %ebp
cmpl $0x2, -0x8(%r15)
jne 0x17b03
movq (%r15), %rdi
callq 0xd640
movq %rax, (%r14,%rbp,8)
incq %rbp
addq $0x10, %r15
cmpq %rbp, %r13
jne 0x1786f
movq %r12, %r15
sarq $0x20, %r15
sarq $0x1e, %r12
movq %r12, %rdi
callq 0xd4c0
movq %rax, %r12
testq %r15, %r15
jle 0x178c5
xorl %r13d, %r13d
movq (%r14,%r13,8), %rdi
callq 0xd210
movl %eax, (%r12,%r13,4)
incq %r13
cmpq %r13, %r15
jne 0x178b0
movl $0x10, %edi
movl $0x1, %esi
callq 0xd380
movq %rax, %r13
leaq 0x10(%rsp), %rdi
movq %rax, %rsi
movq %r14, %rdx
movq %r12, %rcx
movq %r15, %r8
callq 0x17597
testq %r15, %r15
jle 0x17905
xorl %ebp, %ebp
movq (%r14,%rbp,8), %rdi
callq 0xd090
incq %rbp
cmpq %rbp, %r15
jne 0x178f4
movq %r14, %rdi
callq 0xd090
movq %r12, %rdi
callq 0xd090
xorl %r14d, %r14d
testq %r13, %r13
je 0x17934
movq %r13, %rax
cmpq $0x1, (%rax)
movq 0x8(%rax), %rcx
sbbq $-0x1, %r14
movq %rcx, %rax
testq %rcx, %rcx
jne 0x17920
leaq (,%r14,8), %rdi
callq 0xd4c0
movq %rax, %r15
movl $0x1, %esi
movq %r14, %rdi
callq 0xd380
movq %rax, 0x8(%rsp)
testq %r13, %r13
je 0x17990
xorl %r12d, %r12d
movq (%r13), %rdi
testq %rdi, %rdi
je 0x17973
callq 0xd640
movq %rax, (%r15,%r12,8)
incq %r12
movq (%r13), %rdi
movq 0x8(%r13), %rbp
callq 0xd090
movq %r13, %rdi
callq 0xd090
movq %rbp, %r13
testq %rbp, %rbp
jne 0x1795e
leaq -0x1e7(%rip), %rcx      # 0x177b0
movl $0x8, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0xd140
testq %r14, %r14
jle 0x179ff
movq 0x2cb0(%rbx), %r13
xorl %ebp, %ebp
movq $0x0, (%rsp)
xorl %r12d, %r12d
cmpq %r13, %rbp
jge 0x17a07
movq (%r15,%r12,8), %rdi
movq 0x2cb8(%rbx), %rax
movq (%rax,%rbp,8), %rsi
callq 0xd390
testl %eax, %eax
js 0x179f5
je 0x179e4
incq %r12
jmp 0x179f8
movq 0x8(%rsp), %rax
movb $0x1, (%rax,%r12)
incq (%rsp)
incq %r12
incq %rbp
cmpq %r14, %r12
jl 0x179c0
jmp 0x17a07
movq $0x0, (%rsp)
movq %r14, %r13
subq (%rsp), %r13
jle 0x17a8c
addq 0x2cb0(%rbx), %r13
movq 0x2cb8(%rbx), %rdi
leaq (,%r13,8), %rsi
callq 0xd520
movq %rax, 0x2cb8(%rbx)
testq %r14, %r14
je 0x17a6a
movq 0x2cb0(%rbx), %r12
xorl %ebp, %ebp
movq 0x8(%rsp), %rax
cmpb $0x0, (%rax,%rbp)
jne 0x17a62
movq (%r15,%rbp,8), %rdi
callq 0xd640
movq 0x2cb8(%rbx), %rcx
movq %rax, (%rcx,%r12,8)
incq %r12
incq %rbp
cmpq %rbp, %r14
jne 0x17a40
movq %r13, 0x2cb0(%rbx)
movq 0x2cb8(%rbx), %rdi
leaq -0x2cf(%rip), %rcx      # 0x177b0
movl $0x8, %edx
movq %r13, %rsi
callq 0xd140
testq %r14, %r14
je 0x17aa5
xorl %r12d, %r12d
movq (%r15,%r12,8), %rdi
callq 0xd090
incq %r12
cmpq %r12, %r14
jne 0x17a94
movq %r15, %rdi
callq 0xd090
movq 0x8(%rsp), %rdi
callq 0xd090
leaq 0x188c8(%rip), %rsi     # 0x30386
leaq -0x37fc(%rip), %rdx     # 0x142c9
movq %rbx, %rdi
callq 0x126ac
leaq 0x28a0(%rbx), %r15
movq 0x2cb0(%rbx), %rcx
movq 0x2cb8(%rbx), %rsi
xorl %r14d, %r14d
movq %r15, %rdi
xorl %edx, %edx
callq 0x19fd4
movq 0x2860(%rbx), %rdi
movq %r15, %rsi
callq 0x11e3a
jmp 0x177dc
movl $0x2, %r14d
jmp 0x177dc