// File: /workspace/github_c_makefile_star2_O2/Matthias-Wandel[P]imgcomp/src/util.c
// Function: EnsurePathExists
int EnsurePathExists(const char * FileName, int filepath)
{
    char NewPath[PATH_MAX*2];
    int a;
    int LastSlash = 0;

    //printf("Ensure path exists '%s' %d\n",FileName, filepath);

    // Extract the path component of the file name.
    strcpy(NewPath, FileName);
    a = strlen(NewPath);
    
    if (!filepath){
        // If it's not a path to a file, add a trailing slash for the existing
        // code to work properly.
        if (NewPath[a-1] != '/'){
            NewPath[a++] = '/';
        }
    }
    
    for (;;){
        a--;
        if (a == 0){
            NewPath[0] = 0;
            break;    
        }
        if (NewPath[a] == '/'){
            struct stat dummy;
            NewPath[a] = 0;
            if (stat(NewPath, &dummy) == 0){
                if (S_ISDIR(dummy.st_mode)){
                    // Break out of loop, and go forward along path making
                    // the directories.
                    if (LastSlash == 0){
                        // Full path exists.  No need to create any directories.
                        return 1;
                    }
                    break;
                }else{
                    // Its a file.
                    printf("Can't create path '%s' due to file conflict\n",NewPath);
                    fprintf(Log,"Can't create path '%s' due to file conflict\n",NewPath);
                    exit(-1);
                }
            }
            if (LastSlash == 0) LastSlash = a;
        }
    }

    // Now work forward.
    for(;FileName[a];a++){
        //printf("%d/%d: '%c'\n",a,LastSlash,FileName[a]);
        if (FileName[a] == '/' || a == 0){
            if (a == LastSlash) break;
            NewPath[a] = FileName[a];
              #ifdef _WIN32
                if (NewPath[1] == ':' && strlen(NewPath) == 2) continue;
            #endif
            fprintf(Log,"Make directory %s\n",NewPath);
            
            if (mkdir(NewPath,0777)){
                printf("Could not create directory '%s'\n",NewPath);
                fprintf(Log,"Could not create directory '%s'\n",NewPath);
                // Failed to create directory.
                exit(-1);
            }
        }
    }
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20a8, %rsp           # imm = 0x20A8
movl %esi, %ebp
movq %rdi, %rbx
movabsq $-0x100000000, %r13     # imm = 0xFFFFFFFF00000000
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x30a0
movq %r14, %rdi
callq 0x3130
testl %ebp, %ebp
jne 0x8fce
movq %rax, %rcx
shlq $0x20, %rcx
leaq (%rcx,%r13), %rdx
sarq $0x20, %rdx
cmpb $0x2f, 0xa0(%rsp,%rdx)
je 0x8fce
incq %rax
sarq $0x20, %rcx
movb $0x2f, 0xa0(%rsp,%rcx)
movslq %eax, %r14
xorl %ecx, %ecx
leaq 0xa0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %r14, %r12
shlq $0x20, %r12
movq %r14, %rax
cmpq $0x1, %rax
je 0x9036
addq %r13, %r12
leaq -0x1(%rax), %r14
cmpb $0x2f, 0x9f(%rsp,%rax)
movq %r14, %rax
jne 0x8fea
movl %ecx, 0xc(%rsp)
movb $0x0, 0xa0(%rsp,%r14)
movq %rdi, %r15
movq %rsi, %rbp
callq 0x32c0
testl %eax, %eax
je 0x90a5
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
cmovel %r14d, %ecx
movq %r15, %rdi
movq %rbp, %rsi
jmp 0x8fe0
movb $0x0, 0xa0(%rsp)
xorl %r12d, %r12d
movslq %ecx, %rbp
leaq 0x9f7d(%rip), %r13      # 0x12fc8
leaq 0x520e(%rip), %r14      # 0xe260
leaq 0xa0(%rsp), %r15
movb (%rbx,%r12), %al
testb %al, %al
je 0x90c6
cmpb $0x2f, %al
setne %cl
testq %r12, %r12
setne %dl
testb %cl, %dl
jne 0x90a0
cmpq %r12, %rbp
je 0x90c6
movb %al, 0xa0(%rsp,%r12)
movq (%r13), %rdi
movq %r14, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x32a0
movq %r15, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x30b0
testl %eax, %eax
jne 0x90db
incq %r12
jmp 0x905a
movl $0xf000, %eax           # imm = 0xF000
andl 0x28(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
movl 0xc(%rsp), %ecx
jne 0x9110
testl %ecx, %ecx
je 0x90c6
sarq $0x20, %r12
jmp 0x9041
pushq $0x1
popq %rax
addq $0x20a8, %rsp           # imm = 0x20A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5191(%rip), %rbx      # 0xe273
leaq 0xa0(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x3170
movq (%r13), %rdi
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x32a0
pushq $-0x1
popq %rdi
callq 0x34a0
leaq 0x511c(%rip), %rbx      # 0xe233
leaq 0xa0(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x3170
leaq 0x9e95(%rip), %rax      # 0x12fc8
movq (%rax), %rdi
jmp 0x90fb