// File: /workspace/github_c_makefile_star2_O1/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 0x9955
shlq $0x20, %rax
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
addq %rax, %rdx
sarq $0x20, %rdx
cmpb $0x2f, 0x90(%rsp,%rdx)
je 0x9955
incl %ecx
sarq $0x20, %rax
movb $0x2f, 0x90(%rsp,%rax)
movl %ecx, %r12d
decl %r12d
je 0x99d2
movslq %ecx, %r12
decq %r12
xorl %ebp, %ebp
leaq 0x90(%rsp), %r14
movq %rsp, %r15
movl $0xf000, %r13d          # imm = 0xF000
cmpb $0x2f, 0x90(%rsp,%r12)
jne 0x99c8
movb $0x0, 0x90(%rsp,%r12)
movq %r14, %rdi
movq %r15, %rsi
callq 0x32b0
testl %eax, %eax
je 0x99a3
testl %ebp, %ebp
cmovel %r12d, %ebp
xorl %eax, %eax
jmp 0x99c0
movl 0x18(%rsp), %eax
andl %r13d, %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x9a8a
cmpl $0x1, %ebp
movl $0x2, %eax
sbbl $0x0, %eax
testl %eax, %eax
jne 0x9a4c
decq %r12
jne 0x9976
xorl %r12d, %r12d
jmp 0x99d4
xorl %ebp, %ebp
movb $0x0, 0x90(%rsp)
movslq %r12d, %r13
movb (%rbx,%r13), %al
testb %al, %al
je 0x9a51
movslq %ebp, %rbp
leaq 0x95c7(%rip), %r12      # 0x12fb8
leaq 0x4828(%rip), %r14      # 0xe220
leaq 0x90(%rsp), %r15
cmpb $0x2f, %al
setne %cl
testq %r13, %r13
setne %dl
testb %cl, %dl
jne 0x9a3e
cmpq %r13, %rbp
je 0x9a51
movb %al, 0x90(%rsp,%r13)
movq (%r12), %rdi
movq %r14, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x3290
movq %r15, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x30b0
testl %eax, %eax
jne 0x9a68
movb 0x1(%rbx,%r13), %al
incq %r13
testb %al, %al
jne 0x9a00
jmp 0x9a51
cmpl $0x2, %eax
je 0x99dc
movl $0x1, %eax
addq $0x2098, %rsp           # imm = 0x2098
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x47c4(%rip), %rbx      # 0xe233
leaq 0x90(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x3170
movq (%r12), %rdi
jmp 0x9ab0
leaq 0x4762(%rip), %rbx      # 0xe1f3
leaq 0x90(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x3170
leaq 0x950b(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x3290
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480