// File: /workspace/github_c_makefile_star2_O2/salextpuru[P]speccy-toochain/src/mhmt/mhmt-main.c
// Function: do_files
ULONG do_files(void)
{
    char * pack_ext;
    char * depk_ext;
    LONG ext_pos;

    // if there is no output filename, create it
    if( !wrk.fname_out ) {
        depk_ext = ".dpk";

        if( wrk.packtype==PK_MLZ )
            pack_ext = ".mlz";

        else if( wrk.packtype==PK_HRM )
            pack_ext = ".hrm";

        else if( wrk.packtype==PK_HST )
            pack_ext = ".hst";

        else
            pack_ext = ".pak"; // all have the same size, as well as depk_ext - 4 bytes!

        wrk.fname_out = (char *)malloc( 5 + strlen(wrk.fname_in) );

        if( !wrk.fname_out ) {
            printf("Can't allocate memory for output filename!\n");
            return 0;
        }

        strcpy(wrk.fname_out, wrk.fname_in);

        if( !wrk.mode ) { // packing
            strcat(wrk.fname_out, pack_ext);

        } else { // depacking
            ext_pos = strlen( wrk.fname_out ) - 4;

            if( (ext_pos>=0) && (!strcmp(&wrk.fname_out[ext_pos], pack_ext)) )
                strcpy( &wrk.fname_out[ext_pos], depk_ext );

            else
                strcat( wrk.fname_out, depk_ext );
        }
    }

    //open files
    wrk.file_in=fopen(wrk.fname_in,"rb");

    if(!wrk.file_in) {
        printf("Cannot open input file \"%s\"!\n",wrk.fname_in);
        return 0;
    }

    wrk.file_out=fopen(wrk.fname_out,"wb");

    if(!wrk.file_out) {
        printf("Cannot create output file \"%s\"!\n",wrk.fname_out);
        return 0;
    }

    // get length of input file
    if( fseek(wrk.file_in,0,SEEK_END) ) {
        printf("Cannot fseek() input file \"%s\"!\n",wrk.fname_in);
        return 0;
    }

    wrk.inlen=(ULONG)ftell(wrk.file_in);

    if( wrk.inlen==(ULONG)(-1L)  ) {
        printf("Cannot ftell() length of input file \"%s\"!\n",wrk.fname_in);
        wrk.inlen=0;
        return 0;

    } else if( wrk.inlen<16 ) {
        printf("Input file \"%s\" is smaller than 16 bytes - I won't process it!\n",wrk.fname_in);
        return 0;
    }

    if( fseek(wrk.file_in,0,SEEK_SET) ) {
        printf("Cannot fseek() input file \"%s\"!\n",wrk.fname_in);
        return 0;
    }

    // load input file in mem
    wrk.indata=(UBYTE *)malloc(wrk.inlen);

    if( !wrk.indata ) {
        printf("Cannot allocate %d bytes of memory for loading input file \"%s\"!\n", wrk.inlen, wrk.fname_in);
        return 0;
    }

    if( wrk.inlen!=fread(wrk.indata,1,wrk.inlen,wrk.file_in) ) {
        printf("Cannot successfully load input file \"%s\" in memory!\n",wrk.fname_in);
        return 0;
    }

    return 1;// no errors
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
leaq 0x77fd(%rip), %r13      # 0xa320
cmpq $0x0, 0x28(%r13)
je 0x2b84
movq 0x20(%r13), %rbx
leaq 0x2b09(%rip), %rsi      # 0x563e
movq %rbx, %rdi
callq 0x10a0
movq %rax, 0x30(%r13)
testq %rax, %rax
je 0x2ba1
movq 0x28(%r13), %rdi
leaq 0x2b0e(%rip), %rsi      # 0x565f
callq 0x10a0
movq %rax, 0x38(%r13)
testq %rax, %rax
je 0x2bae
movq 0x30(%r13), %rdi
xorl %ebx, %ebx
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x1090
testl %eax, %eax
je 0x2c5e
movq 0x20(%r13), %rsi
leaq 0x2b01(%rip), %rdi      # 0x5683
jmp 0x2bbb
movl (%r13), %eax
decl %eax
cmpl $0x2, %eax
ja 0x2bce
movl %eax, %eax
leaq 0x31f4(%rip), %rcx      # 0x5d8c
movslq (%rcx,%rax,4), %r14
addq %rcx, %r14
jmp 0x2bd5
movq 0x20(%r13), %rsi
leaq 0x2a95(%rip), %rdi      # 0x5641
jmp 0x2bb9
movq 0x28(%r13), %rsi
leaq 0x2aa9(%rip), %rdi      # 0x5662
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1040
movl %ebx, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x2a64(%rip), %r14      # 0x5639
movq 0x20(%r13), %rbx
movq %rbx, %rdi
callq 0x1050
leaq 0x5(%rax), %rdi
callq 0x10d0
movq %rax, 0x28(%r13)
testq %rax, %rax
je 0x2c4b
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1060
cmpl $0x0, 0x8(%r13)
je 0x2c85
movq %r15, %rdi
callq 0x1050
addl $-0x4, %eax
js 0x2c30
movl %eax, %eax
movq %r15, %r12
addq %rax, %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x10f0
testl %eax, %eax
je 0x2d0b
movq %r15, %rdi
callq 0x1050
movl $0x6b70642e, (%r15,%rax) # imm = 0x6B70642E
movb $0x0, 0x4(%r15,%rax)
jmp 0x2b2e
leaq 0x2ffd(%rip), %rdi      # 0x5c4f
callq 0x1110
xorl %ebx, %ebx
jmp 0x2bc2
movq 0x30(%r13), %rdi
callq 0x1030
movl %eax, 0x48(%r13)
cmpl $-0x1, %eax
je 0x2ca2
cmpl $0xf, %eax
ja 0x2cc0
movq 0x20(%r13), %rsi
leaq 0x2a4f(%rip), %rdi      # 0x56cf
jmp 0x2bb9
movq %r15, %rdi
callq 0x1050
movl (%r14), %ecx
movl %ecx, (%r15,%rax)
movb 0x4(%r14), %cl
movb %cl, 0x4(%r15,%rax)
jmp 0x2b2e
movq 0x20(%r13), %rsi
leaq 0x29f7(%rip), %rdi      # 0x56a4
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1040
andl $0x0, 0x48(%r13)
jmp 0x2bc2
movq 0x30(%r13), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
xorl %edx, %edx
callq 0x1090
testl %eax, %eax
jne 0x2b77
movl 0x48(%r13), %r14d
movq %r14, %rdi
callq 0x10d0
movq %rax, 0x40(%r13)
testq %rax, %rax
je 0x2d1e
movq 0x30(%r13), %rcx
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r14, %rdx
callq 0x1100
cmpq %r14, %rax
jne 0x2d3a
pushq $0x1
popq %rbx
jmp 0x2bc2
movb $0x0, 0x4(%r12)
movl $0x6b70642e, (%r12)     # imm = 0x6B70642E
jmp 0x2b2e
movq 0x20(%r13), %rdx
leaq 0x29e6(%rip), %rdi      # 0x570f
xorl %ebx, %ebx
movl %r14d, %esi
xorl %eax, %eax
callq 0x1040
jmp 0x2bc2
movq 0x20(%r13), %rsi
leaq 0x2a0b(%rip), %rdi      # 0x5750
jmp 0x2bb9