// File: /workspace/github_c_makefile_star2_O3/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 $0x428, %rsp            # imm = 0x428
movl $0x3, %r14d
cmpq $0x0, 0x27f8(%rsi)
je 0x173be
movl %r14d, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %r15
movl $0x2, %r14d
cmpq $0x0, 0x208(%rdi)
jle 0x173a9
movq 0x200(%r15), %rax
cmpl $0x2, (%rax)
jne 0x173a9
movq %rsi, %rbx
movq 0x8(%rax), %rcx
leaq 0x1957b(%rip), %rdx     # 0x30966
leaq 0x20(%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 0x1745f
movq 0x200(%r15), %r15
decq %r13
addq $0x18, %r15
xorl %ebp, %ebp
cmpl $0x2, -0x8(%r15)
jne 0x176e7
movq (%r15), %rdi
callq 0xd640
movq %rax, (%r14,%rbp,8)
incq %rbp
addq $0x10, %r15
cmpq %rbp, %r13
jne 0x1743c
movq %rbx, 0x18(%rsp)
movq %r12, %r15
sarq $0x20, %r15
sarq $0x1e, %r12
movq %r12, %rdi
callq 0xd4c0
movq %rax, %r12
testq %r15, %r15
jle 0x17497
xorl %r13d, %r13d
movq (%r14,%r13,8), %rdi
callq 0xd210
movl %eax, (%r12,%r13,4)
incq %r13
cmpq %r13, %r15
jne 0x17482
movl $0x10, %edi
movl $0x1, %esi
callq 0xd380
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
movq %rax, %rsi
movq %r14, %rdx
movq %r12, %rcx
movq %r15, %r8
callq 0x1716e
testq %r15, %r15
jle 0x174d7
xorl %ebp, %ebp
movq (%r14,%rbp,8), %rdi
callq 0xd090
incq %rbp
cmpq %rbp, %r15
jne 0x174c6
movq %r14, %rdi
callq 0xd090
movq %r12, %rdi
callq 0xd090
xorl %r14d, %r14d
testq %rbx, %rbx
je 0x17506
movq %rbx, %rax
cmpq $0x1, (%rax)
movq 0x8(%rax), %rcx
sbbq $-0x1, %r14
movq %rcx, %rax
testq %rcx, %rcx
jne 0x174f2
leaq (,%r14,8), %rdi
callq 0xd4c0
movq %rax, %r15
movl $0x1, %esi
movq %r14, %rdi
callq 0xd380
movq %rax, 0x10(%rsp)
testq %rbx, %rbx
je 0x17563
xorl %r12d, %r12d
movq (%rbx), %rbp
testq %rbp, %rbp
je 0x17547
movq %rbp, %rdi
callq 0xd640
movq %rax, (%r15,%r12,8)
incq %r12
movq 0x8(%rbx), %r13
movq %rbp, %rdi
callq 0xd090
movq %rbx, %rdi
callq 0xd090
movq %r13, %rbx
testq %r13, %r13
jne 0x17530
leaq -0x1ed(%rip), %rcx      # 0x1737d
movl $0x8, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0xd140
testq %r14, %r14
jle 0x175d9
movq 0x18(%rsp), %rbx
movq 0x2cb0(%rbx), %r13
xorl %ebp, %ebp
movq $0x0, 0x8(%rsp)
xorl %r12d, %r12d
cmpq %r13, %rbp
jge 0x175e7
movq (%r15,%r12,8), %rdi
movq 0x2cb8(%rbx), %rax
movq (%rax,%rbp,8), %rsi
callq 0xd390
testl %eax, %eax
js 0x175cf
je 0x175bd
incq %r12
jmp 0x175d2
movq 0x10(%rsp), %rax
movb $0x1, (%rax,%r12)
incq 0x8(%rsp)
incq %r12
incq %rbp
cmpq %r14, %r12
jl 0x17599
jmp 0x175e7
movq $0x0, 0x8(%rsp)
movq 0x18(%rsp), %rbx
movq %r14, %r13
subq 0x8(%rsp), %r13
jle 0x17670
addq 0x2cb0(%rbx), %r13
movq 0x2cb8(%rbx), %rdi
leaq (,%r13,8), %rsi
callq 0xd520
movq %rax, 0x2cb8(%rbx)
testq %r14, %r14
je 0x17652
movq 0x2cb0(%rbx), %r12
xorl %ebp, %ebp
movq 0x10(%rsp), %rax
cmpb $0x0, (%rax,%rbp)
jne 0x17643
movq (%r15,%rbp,8), %rdi
callq 0xd640
movq 0x2cb8(%rbx), %rcx
movq %rax, (%rcx,%r12,8)
incq %r12
incq %rbp
cmpq %rbp, %r14
jne 0x17621
movq 0x2cb8(%rbx), %rax
movq %r13, 0x2cb0(%rbx)
leaq -0x2e3(%rip), %rcx      # 0x1737d
movl $0x8, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0xd140
testq %r14, %r14
je 0x17689
xorl %r12d, %r12d
movq (%r15,%r12,8), %rdi
callq 0xd090
incq %r12
cmpq %r12, %r14
jne 0x17678
movq %r15, %rdi
callq 0xd090
movq 0x10(%rsp), %rdi
callq 0xd090
leaq 0x17ce1(%rip), %rsi     # 0x2f383
leaq -0x3682(%rip), %rdx     # 0x14027
movq %rbx, %rdi
callq 0x12487
leaq 0x28a0(%rbx), %r15
movq 0x2cb0(%rbx), %rcx
movq 0x2cb8(%rbx), %rsi
xorl %r14d, %r14d
movq %r15, %rdi
xorl %edx, %edx
callq 0x1994c
movq 0x2860(%rbx), %rdi
movq %r15, %rsi
callq 0x11c24
jmp 0x173a9
movl $0x2, %r14d
jmp 0x173a9