// File: /workspace/github_c_makefile_star2_O0/tstword[P]MIPSAssemblerC/src/main.c
// Function: main
int main(int argc, char *argv[]) {
    const char *output_file = "a.obj";
    const char *text_file = NULL;
    const char *data_file = NULL;
    int assemble_only = 0, display_help = 0;
    
    const char **input_array;
    size_t input_count;
    
#ifndef _WIN32
    int opt;
    while((opt = getopt(argc, argv, "aho:t:d:")) != -1) {
        switch(opt) {
            case 'a':
                assemble_only = 1;
                break;
            case 'h':
                display_help = 1;
                break;
            case 't':
                text_file = optarg;
                break;
            case 'd':
                data_file = optarg;
                break;
            case 'o':
                output_file = optarg;
                break;
            default: /* '?' */
                fprintf(stderr, "\nSee '%s -h' for more information\n", argv[0]);
                return EXIT_FAILURE;
        }
    }
    input_array = (const char **)(argv + optind);
    input_count = argc - optind;
#else
    /* Grr we have to parse manually, sloppy but will get the job done */
    /* We could use this parsing algorithm for Linux / Mac, but I trust getopt more */
    /* Windows users will have to report errors (if any) */

    input_array = (const char **)malloc(sizeof(const char *) * argc);
    
    if(input_array == NULL) { 
        perror("CRITICAL ERROR: Failed to allocate memory input_array: ");
        exit(EXIT_FAILURE);
    }
    
    input_count = 0;

    int skip_index = 0, ch;

    for(int i = 1; i < argc; ++i) {
        if(argv[i][0] == '-') {
            skip_index = 0;
            while((ch = *(++argv[i])) != '\0') {
                switch(ch) {
                    case 'a':
                        assemble_only = 1;
                        break;
                    case 'h':
                        display_help = 1;
                        break;
                    case 'o':
                        if(i + 1 == argc || argv[i + 1][0] == '-') {
                            fprintf(stderr, "%s: option requires an argument -- 'o'\n", argv[0]);
                            return EXIT_FAILURE;
                        }
                        output_file = argv[i + 1];
                        skip_index = 1;
                        break;
                    case 't':
                        if(i + 1 == argc || argv[i + 1][0] == '-') {
                            fprintf(stderr, "%s: option requires an argument -- 't'\n", argv[0]);
                            return EXIT_FAILURE;
                        }
                        text_file = argv[i + 1];
                        skip_index = 1;
                        break;
                    case 'd':
                        if(i + 1 == argc || argv[i + 1][0] == '-') {
                            fprintf(stderr, "%s: option requires an argument -- 'd'\n", argv[0]);
                            return EXIT_FAILURE;
                        }
                        data_file = argv[i + 1];
                        skip_index = 1;
                        break;
                    default: /* ? */
                        fprintf(stderr, "%s: invalid option -- '%c'\n", argv[0], ch);
                        fprintf(stderr, "\nSee '%s -h' for more information\n", argv[0]);
                        return EXIT_FAILURE;
                }
            }
            if(skip_index) ++i;
        }
        else {
            input_array[input_count++] = (const char *)argv[i];
        }
    }
#endif

    if(display_help) display_help_msg(argv[0]);

    if(input_count == 0) {
        fprintf(stderr, "%s: Error: no input files\n", argv[0]);
        fprintf(stderr, "\nSee '%s -h' for more information\n", argv[0]);
        return EXIT_FAILURE;
    }

    struct assembler *assembler = create_assembler();
    astatus_t status = execute_assembler(assembler, input_array, input_count);

#ifdef _WIN32
    free((void *)input_array);   /* I'm annoyed that I have to do this but oh well, nothing to do right now. */
#endif
    if(status != ASSEMBLER_STATUS_OK) {
        fprintf(stderr, "\nFailed to assemble program\n");
        destroy_assembler(&assembler);
        return EXIT_FAILURE;
    }
    else if(!assemble_only) {
        write_object_file(assembler, output_file);
    }

    /* Dump segments to file if specified */
    if(text_file != NULL) dump_segment(assembler, SEGMENT_TEXT, text_file);
    if(data_file != NULL) dump_segment(assembler, SEGMENT_DATA, data_file);

    destroy_assembler(&assembler);

    return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0x3550(%rip), %rax      # 0xab7d
movq %rax, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x3526(%rip), %rdx      # 0xab83
callq 0x3130
movl %eax, -0x44(%rbp)
cmpl $-0x1, %eax
je 0x7705
movl -0x44(%rbp), %eax
addl $-0x61, %eax
movl %eax, %ecx
movq %rcx, -0x60(%rbp)
subl $0x13, %eax
ja 0x76d5
movq -0x60(%rbp), %rax
leaq 0x324e(%rip), %rcx      # 0xa8d8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x1, -0x2c(%rbp)
jmp 0x7700
movl $0x1, -0x30(%rbp)
jmp 0x7700
movq 0x692c(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x7700
movq 0x691c(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x7700
movq 0x690c(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x7700
movq 0x691c(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x349f(%rip), %rsi      # 0xab8c
movb $0x0, %al
callq 0x30c0
movl $0x1, -0x4(%rbp)
jmp 0x7839
jmp 0x764f
movq -0x10(%rbp), %rax
movq 0x68c0(%rip), %rcx      # 0xdfd0
movslq (%rcx), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl -0x8(%rbp), %eax
movq 0x68a8(%rip), %rcx      # 0xdfd0
subl (%rcx), %eax
cltq
movq %rax, -0x40(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x7742
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x7520
cmpq $0x0, -0x40(%rbp)
jne 0x7793
movq 0x68a8(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x344e(%rip), %rsi      # 0xabaf
movb $0x0, %al
callq 0x30c0
movq 0x6889(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x340c(%rip), %rsi      # 0xab8c
movb $0x0, %al
callq 0x30c0
movl $0x1, -0x4(%rbp)
jmp 0x7839
movb $0x0, %al
callq 0x6e00
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x6f10
movb %al, -0x51(%rbp)
movzbl -0x51(%rbp), %eax
cmpl $0x1, %eax
je 0x77e5
movq 0x6836(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x33fe(%rip), %rsi      # 0xabca
movb $0x0, %al
callq 0x30c0
leaq -0x50(%rbp), %rdi
callq 0x7110
movl $0x1, -0x4(%rbp)
jmp 0x7839
cmpl $0x0, -0x2c(%rbp)
jne 0x77f8
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x7850
jmp 0x77fa
cmpq $0x0, -0x20(%rbp)
je 0x7810
movq -0x50(%rbp), %rdi
movq -0x20(%rbp), %rdx
xorl %esi, %esi
callq 0x7a80
cmpq $0x0, -0x28(%rbp)
je 0x7829
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rdx
movl $0x1, %esi
callq 0x7a80
leaq -0x50(%rbp), %rdi
callq 0x7110
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)