// File: /workspace/github_c_makefile_star2_O3/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 $0x2098, %rsp           # imm = 0x2098
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x90(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x30a0
movq %r14, %rdi
callq 0x3130
movl %eax, %ecx
testl %ebp, %ebp
jne 0x9949
shlq $0x20, %rax
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
addq %rax, %rdx
sarq $0x20, %rdx
cmpb $0x2f, 0x90(%rsp,%rdx)
je 0x9949
incl %ecx
sarq $0x20, %rax
movb $0x2f, 0x90(%rsp,%rax)
xorl %ebp, %ebp
cmpl $0x1, %ecx
je 0x99a8
movabsq $-0x100000000, %r13     # imm = 0xFFFFFFFF00000000
movslq %ecx, %r12
leaq -0x1(%r12), %r14
shlq $0x20, %r12
addq %r13, %r12
xorl %ebp, %ebp
movq %rsp, %r15
cmpb $0x2f, 0x90(%rsp,%r14)
jne 0x99a0
movb $0x0, 0x90(%rsp,%r14)
leaq 0x90(%rsp), %rdi
movq %r15, %rsi
callq 0x32c0
testl %eax, %eax
je 0x9a35
testl %ebp, %ebp
cmovel %r14d, %ebp
addq %r13, %r12
decq %r14
jne 0x996e
movb $0x0, 0x90(%rsp)
xorl %r12d, %r12d
movb (%rbx,%r12), %al
testb %al, %al
je 0x9a1e
movslq %ebp, %rbp
leaq 0x95f3(%rip), %r13      # 0x12fb8
leaq 0x48c4(%rip), %r14      # 0xe290
leaq 0x90(%rsp), %r15
cmpb $0x2f, %al
setne %cl
testq %r12, %r12
setne %dl
testb %cl, %dl
jne 0x9a12
cmpq %r12, %rbp
je 0x9a1e
movb %al, 0x90(%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 0x9a52
movb 0x1(%rbx,%r12), %al
incq %r12
testb %al, %al
jne 0x99d4
movl $0x1, %eax
addq $0x2098, %rsp           # imm = 0x2098
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xf000, %eax           # imm = 0xF000
andl 0x18(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x9a89
testl %ebp, %ebp
je 0x9a1e
sarq $0x20, %r12
jmp 0x99b3
leaq 0x484a(%rip), %rbx      # 0xe2a3
leaq 0x90(%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
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3490
leaq 0x47d3(%rip), %rbx      # 0xe263
leaq 0x90(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x3170
leaq 0x950c(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
jmp 0x9a72