// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
pushq %rax
leaq 0x8366(%rip), %r12      # 0xb320
cmpq $0x0, 0x28(%r12)
je 0x3021
movq 0x20(%r12), %rdi
leaq 0x3670(%rip), %rsi      # 0x663e
callq 0x10b0
movq %rax, 0x30(%r12)
testq %rax, %rax
je 0x303e
movq 0x28(%r12), %rdi
leaq 0x3676(%rip), %rsi      # 0x665f
callq 0x10b0
movq %rax, 0x38(%r12)
testq %rax, %rax
je 0x304c
movq 0x30(%r12), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
movl $0x2, %edx
callq 0x10a0
testl %eax, %eax
je 0x3106
movq 0x20(%r12), %rsi
leaq 0x3664(%rip), %rdi      # 0x6683
jmp 0x305a
movl (%r12), %eax
decl %eax
cmpl $0x2, %eax
ja 0x306f
movl %eax, %eax
leaq 0x3d57(%rip), %rcx      # 0x6d8c
movslq (%rcx,%rax,4), %rbx
addq %rcx, %rbx
jmp 0x3076
movq 0x20(%r12), %rsi
leaq 0x35f7(%rip), %rdi      # 0x6641
jmp 0x3058
movq 0x28(%r12), %rsi
leaq 0x360a(%rip), %rdi      # 0x6662
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1040
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0x35c3(%rip), %rbx      # 0x6639
movq 0x20(%r12), %r15
movq %r15, %rdi
callq 0x1050
leaq 0x5(%rax), %rdi
callq 0x10e0
movq %rax, 0x28(%r12)
testq %rax, %rax
je 0x30f3
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
callq 0x1070
cmpl $0x0, 0x8(%r12)
je 0x3130
movq %r14, %rdi
callq 0x1050
addl $-0x4, %eax
js 0x30d8
movl %eax, %eax
movq %r14, %r15
addq %rax, %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1100
testl %eax, %eax
je 0x31d1
movq %r14, %rdi
callq 0x1050
movl $0x6b70642e, (%r14,%rax) # imm = 0x6B70642E
movb $0x0, 0x4(%r14,%rax)
jmp 0x2fc2
leaq 0x3b55(%rip), %rdi      # 0x6c4f
callq 0x1120
xorl %ebx, %ebx
jmp 0x3061
movq 0x30(%r12), %rdi
callq 0x1030
movl %eax, 0x48(%r12)
cmpl $-0x1, %eax
je 0x314b
cmpl $0xf, %eax
ja 0x316e
movq 0x20(%r12), %rsi
leaq 0x35a4(%rip), %rdi      # 0x66cf
jmp 0x3058
movq %r14, %rdi
callq 0x1050
movl (%rbx), %ecx
movl %ecx, (%r14,%rax)
movb 0x4(%rbx), %cl
movb %cl, 0x4(%r14,%rax)
jmp 0x2fc2
movq 0x20(%r12), %rsi
leaq 0x354d(%rip), %rdi      # 0x66a4
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1040
movl $0x0, 0x48(%r12)
jmp 0x3061
movq 0x30(%r12), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
xorl %edx, %edx
callq 0x10a0
testl %eax, %eax
jne 0x3013
movl 0x48(%r12), %r14d
movq %r14, %rdi
callq 0x10e0
movq %rax, 0x40(%r12)
testq %rax, %rax
je 0x31e2
movq 0x30(%r12), %rcx
movl $0x1, %esi
movq %rax, %rdi
movq %r14, %rdx
callq 0x1110
movl $0x1, %ebx
cmpq %r14, %rax
je 0x3061
movq 0x20(%r12), %rsi
leaq 0x3584(%rip), %rdi      # 0x6750
jmp 0x3058
movb $0x0, 0x4(%r15)
movl $0x6b70642e, (%r15)     # imm = 0x6B70642E
jmp 0x2fc2
movq 0x20(%r12), %rdx
leaq 0x3521(%rip), %rdi      # 0x670f
xorl %ebx, %ebx
movl %r14d, %esi
xorl %eax, %eax
callq 0x1040
jmp 0x3061