// File: /workspace/github_c_makefile_star2_O0/inflex[P]ripMIME/mime.c
// Function: MIME_test_uniquename
int MIME_test_uniquename( char *path, char *fname, int method )
{
    struct stat buf;

    char newname[_MIME_STRLEN_MAX +1];
    char scr[_MIME_STRLEN_MAX +1]; /** Scratch var **/
    char *frontname, *extention;

    int cleared = 0;
    int count = 1;

    int randval = get_random_value();

    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_test_uniquename:DEBUG: Start (%s)",FL,fname);

    frontname = extention = NULL;  // shuts the compiler up

    if (method == _MIME_RENAME_METHOD_INFIX)
    {
        PLD_strncpy(scr,fname, _MIMEH_STRLEN_MAX);
        frontname = scr;
        extention = strrchr(scr,'.');

        if (extention)
        {
            *extention = '\0';
            extention++;
        }
        else
        {
            method = _MIME_RENAME_METHOD_POSTFIX;
        }
    }

  if (method == _MIME_RENAME_METHOD_RANDINFIX)
    {
        PLD_strncpy(scr,fname, _MIMEH_STRLEN_MAX);
        frontname = scr;
        extention = strrchr(scr,'.');

        if (extention)
        {
            *extention = '\0';
            extention++;
        }
        else
        {
            method = _MIME_RENAME_METHOD_RANDPOSTFIX;
        }
    }

    snprintf(newname,_MIME_STRLEN_MAX,"%s/%s",path,fname);
    while (!cleared)
    {
        if ((stat(newname, &buf) == -1))
        {
            cleared++;
        }
        else
        {
            if (method == _MIME_RENAME_METHOD_PREFIX)
            {
                snprintf(newname,_MIME_STRLEN_MAX,"%s/%d_%s",path,count,fname);
            }
            else
                if (method == _MIME_RENAME_METHOD_INFIX)
                {
                    snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d.%s",path,frontname,count,extention);
                }
                else
                    if (method == _MIME_RENAME_METHOD_POSTFIX)
                    {
                        snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d",path,fname,count);
                    }
                    else
                        /* Handle randome methots*/
                        if (method == _MIME_RENAME_METHOD_RANDPREFIX)
                        {
                            snprintf(newname,_MIME_STRLEN_MAX,"%s/%d_%d_%s",path,count,randval,fname);
                        }
                        else
                            if (method == _MIME_RENAME_METHOD_RANDINFIX)
                            {
                                snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d_%d.%s",path,frontname,count,randval,extention);
                            }
                            else
                                if (method == _MIME_RENAME_METHOD_RANDPOSTFIX)
                                {
                                    snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d_%d",path,fname,count,randval);
                                }
            count++;
        }
    }
    if (count > 1)
    {
        frontname = strrchr(newname,'/');
        if (frontname) frontname++;
        else frontname = newname;

        PLD_strncpy(fname, frontname, _MIMEH_FILENAMELEN_MAX); //FIXME - this assumes that the buffer space is at least MIME_STRLEN_MAX sized.
    }
    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_test_uniquename:DEBUG: Done (%s)",FL,fname);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x8e0, %rsp            # imm = 0x8E0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl $0x0, -0x8c4(%rbp)
movl $0x1, -0x8c8(%rbp)
callq 0x4140
movl %eax, -0x8cc(%rbp)
cmpl $0x1, 0x23514(%rip)     # 0x276f0
jl 0x41fc
movq -0x10(%rbp), %rcx
leaq 0x1549e(%rip), %rdi     # 0x19687
leaq 0x152df(%rip), %rsi     # 0x194cf
movl $0x3c8, %edx            # imm = 0x3C8
movb $0x0, %al
callq 0x117c0
movq $0x0, -0x8c0(%rbp)
movq $0x0, -0x8b8(%rbp)
cmpl $0x1, -0x14(%rbp)
jne 0x4284
leaq -0x8b0(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x27f6, %edx           # imm = 0x27F6
callq 0x10640
leaq -0x8b0(%rbp), %rax
movq %rax, -0x8b8(%rbp)
leaq -0x8b0(%rbp), %rdi
movl $0x2e, %esi
callq 0x2140
movq %rax, -0x8c0(%rbp)
cmpq $0x0, -0x8c0(%rbp)
je 0x427b
movq -0x8c0(%rbp), %rax
movb $0x0, (%rax)
movq -0x8c0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8c0(%rbp)
jmp 0x4282
movl $0x3, -0x14(%rbp)
jmp 0x4284
cmpl $0x4, -0x14(%rbp)
jne 0x42f6
leaq -0x8b0(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x27f6, %edx           # imm = 0x27F6
callq 0x10640
leaq -0x8b0(%rbp), %rax
movq %rax, -0x8b8(%rbp)
leaq -0x8b0(%rbp), %rdi
movl $0x2e, %esi
callq 0x2140
movq %rax, -0x8c0(%rbp)
cmpq $0x0, -0x8c0(%rbp)
je 0x42ed
movq -0x8c0(%rbp), %rax
movb $0x0, (%rax)
movq -0x8c0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8c0(%rbp)
jmp 0x42f4
movl $0x6, -0x14(%rbp)
jmp 0x42f6
leaq -0x4b0(%rbp), %rdi
movq -0x8(%rbp), %rcx
movq -0x10(%rbp), %r8
movl $0x3ff, %esi            # imm = 0x3FF
leaq 0x151eb(%rip), %rdx     # 0x194fc
movb $0x0, %al
callq 0x2130
cmpl $0x0, -0x8c4(%rbp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x432d
jmp 0x44e3
leaq -0x4b0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x2230
cmpl $-0x1, %eax
jne 0x4359
movl -0x8c4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c4(%rbp)
jmp 0x44de
cmpl $0x2, -0x14(%rbp)
jne 0x438d
leaq -0x4b0(%rbp), %rdi
movq -0x8(%rbp), %rcx
movl -0x8c8(%rbp), %r8d
movq -0x10(%rbp), %r9
movl $0x3ff, %esi            # imm = 0x3FF
leaq 0x15333(%rip), %rdx     # 0x196b4
movb $0x0, %al
callq 0x2130
jmp 0x44cf
cmpl $0x1, -0x14(%rbp)
jne 0x43cf
leaq -0x4b0(%rbp), %rdi
movq -0x8(%rbp), %rcx
movq -0x8b8(%rbp), %r8
movl -0x8c8(%rbp), %r9d
movq -0x8c0(%rbp), %rax
movl $0x3ff, %esi            # imm = 0x3FF
leaq 0x152fe(%rip), %rdx     # 0x196bd
movq %rax, (%rsp)
movb $0x0, %al
callq 0x2130
jmp 0x44cd
cmpl $0x3, -0x14(%rbp)
jne 0x4403
leaq -0x4b0(%rbp), %rdi
movq -0x8(%rbp), %rcx
movq -0x10(%rbp), %r8
movl -0x8c8(%rbp), %r9d
movl $0x3ff, %esi            # imm = 0x3FF
leaq 0x152d2(%rip), %rdx     # 0x196c9
movb $0x0, %al
callq 0x2130
jmp 0x44cb
cmpl $0x5, -0x14(%rbp)
jne 0x4442
leaq -0x4b0(%rbp), %rdi
movq -0x8(%rbp), %rcx
movl -0x8c8(%rbp), %r8d
movl -0x8cc(%rbp), %r9d
movq -0x10(%rbp), %rax
movl $0x3ff, %esi            # imm = 0x3FF
leaq 0x152a0(%rip), %rdx     # 0x196d2
movq %rax, (%rsp)
movb $0x0, %al
callq 0x2130
jmp 0x44c9
cmpl $0x4, -0x14(%rbp)
jne 0x448d
leaq -0x4b0(%rbp), %rdi
movq -0x8(%rbp), %rcx
movq -0x8b8(%rbp), %r8
movl -0x8c8(%rbp), %r9d
movl -0x8cc(%rbp), %r10d
movq -0x8c0(%rbp), %rax
movl $0x3ff, %esi            # imm = 0x3FF
leaq 0x15263(%rip), %rdx     # 0x196de
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x2130
jmp 0x44c7
cmpl $0x6, -0x14(%rbp)
jne 0x44c5
leaq -0x4b0(%rbp), %rdi
movq -0x8(%rbp), %rcx
movq -0x10(%rbp), %r8
movl -0x8c8(%rbp), %r9d
movl -0x8cc(%rbp), %eax
movl $0x3ff, %esi            # imm = 0x3FF
leaq 0x15232(%rip), %rdx     # 0x196ed
movl %eax, (%rsp)
movb $0x0, %al
callq 0x2130
jmp 0x44c7
jmp 0x44c9
jmp 0x44cb
jmp 0x44cd
jmp 0x44cf
movl -0x8c8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c8(%rbp)
jmp 0x4318
cmpl $0x1, -0x8c8(%rbp)
jle 0x4545
leaq -0x4b0(%rbp), %rdi
movl $0x2f, %esi
callq 0x2140
movq %rax, -0x8b8(%rbp)
cmpq $0x0, -0x8b8(%rbp)
je 0x4522
movq -0x8b8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8b8(%rbp)
jmp 0x4530
leaq -0x4b0(%rbp), %rax
movq %rax, -0x8b8(%rbp)
movq -0x10(%rbp), %rdi
movq -0x8b8(%rbp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x10640
cmpl $0x1, 0x231a4(%rip)     # 0x276f0
jl 0x456c
movq -0x10(%rbp), %rcx
leaq 0x151a0(%rip), %rdi     # 0x196f9
leaq 0x14f6f(%rip), %rsi     # 0x194cf
movl $0x420, %edx            # imm = 0x420
movb $0x0, %al
callq 0x117c0
xorl %eax, %eax
addq $0x8e0, %rsp            # imm = 0x8E0
popq %rbp
retq
nopw (%rax,%rax)