// File: /workspace/github_cpp_makefile_star2_O2/gpertea[P]gclib/GBase.cpp
// Function: Gmkdir(char const*, bool, int)
int Gmkdir(const char *path, bool recursive, int perms) {
	if (path==NULL || path[0]==0) return -1;
	mode_t process_mask = umask(0); //is this really needed?
	if (!recursive) {
	   int r=G_mkdir(path, perms);
	   if (r!=0)
	      GMessage("Warning: G_mkdir(%s) failed: %s\n", path, strerror(errno));
	   umask(process_mask);
	   return r;
	   }
	int plen=strlen(path);
	char* gpath=NULL;
	//make sure gpath ends with /
	if (path[plen-1]=='/') {
		gpath=Gstrdup(path);
	}
	else {
		GMALLOC(gpath, plen+2);
		strcpy(gpath,path);
		strcat(gpath, "/");
		++plen;
	}
	//char* ss=gpath+plen-1;
	char* psep = gpath+plen-1; //start at the last /
	GDynArray<char*> dirstack(4); // stack of directories that should be created
	while (psep>gpath && *(psep-1)=='/') --psep; //skip double slashes
    *psep='\0';
    int fexists=0;
	while ((fexists=fileExists(gpath))==0) {
      dirstack.Push(psep);
      do { --psep; } while (psep>gpath && *psep!='/');
      if (psep<=gpath) { psep=NULL; break; }
      while (psep>gpath && *(psep-1)=='/') --psep;
      *psep='\0';
	}
	if (psep) *psep='/';
	while (dirstack.Count()>0) {
		psep=dirstack.Pop();
		int mkdir_err=0;
		if ((mkdir_err=G_mkdir(gpath, perms))!=0) {
				GMessage("Warning: mkdir(%s) failed: %s\n", gpath, strerror(errno));
			GFREE(gpath);
			umask(process_mask);
			return -1;
		}
		*psep='/';
	}
	GFREE(gpath);
	umask(process_mask);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x30, %rsp
pushq $-0x1
popq %r15
testq %rdi, %rdi
je 0x2aa7
movq %rdi, %r14
cmpb $0x0, (%rdi)
je 0x2aa7
movl %edx, %ebp
movl %esi, %r12d
xorl %edi, %edi
callq 0x2410
movl %eax, %ebx
movq %r14, %rdi
testb %r12b, %r12b
je 0x2908
callq 0x2140
andq $0x0, 0x8(%rsp)
movq %rax, %r15
movq %rax, %r12
shlq $0x20, %r12
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq %r12, %rax
sarq $0x20, %rax
cmpb $0x2f, (%r14,%rax)
jne 0x2942
movq %r14, %rdi
xorl %esi, %esi
callq 0x2704
movq %rax, 0x8(%rsp)
jmp 0x299f
movl %ebp, %esi
callq 0x2150
movl %eax, %r15d
testl %eax, %eax
je 0x2936
callq 0x2040
movl (%rax), %edi
callq 0x20e0
leaq 0x9732(%rip), %rdi      # 0xc05b
movq %r14, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2662
movl %ebx, %edi
callq 0x2410
jmp 0x2aa7
movabsq $0x200000000, %rax      # imm = 0x200000000
addq %rax, %r12
sarq $0x20, %r12
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x415c
testb %al, %al
je 0x2ab7
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x2220
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x2140
movw $0x2f, (%r14,%rax)
movq 0x8(%rsp), %rax
shlq $0x20, %r15
movabsq $0x100000000, %r12      # imm = 0x100000000
addq %r15, %r12
sarq $0x20, %r12
leaq (%rax,%r12), %r14
decq %r14
leaq 0x10(%rsp), %rdi
pushq $0x4
popq %rsi
callq 0x4180
movq 0x8(%rsp), %rax
movq %r14, %r15
cmpq %rax, %r14
jbe 0x29cf
leaq -0x1(%r15), %r14
cmpb $0x2f, -0x1(%r15)
je 0x29bc
leaq 0x10(%rsp), %r14
movb $0x0, (%r15)
movq 0x8(%rsp), %rdi
callq 0x2ada
testl %eax, %eax
jne 0x2a27
movq %r14, %rdi
movq %r15, %rsi
callq 0x41c2
movq 0x8(%rsp), %rax
decq %r15
cmpq %rax, %r15
jbe 0x2a50
leaq -0x1(%r15), %rcx
cmpb $0x2f, (%r15)
movq %rcx, %r15
jne 0x29f9
incq %rcx
cmpq %rax, %rcx
jbe 0x2a22
cmpb $0x2f, -0x1(%rcx)
movq %rcx, %r15
leaq -0x1(%rcx), %rcx
je 0x2a0e
jmp 0x29d4
movq %rcx, %r15
jmp 0x29d4
movb $0x2f, (%r15)
jmp 0x2a50
decl %eax
movl %eax, 0x28(%rsp)
movq 0x20(%rsp), %rcx
movq (%rcx,%rax,8), %r14
movq 0x8(%rsp), %rdi
movl %ebp, %esi
callq 0x2150
testl %eax, %eax
jne 0x2a5d
movb $0x2f, (%r14)
movl 0x28(%rsp), %eax
testl %eax, %eax
jne 0x2a2d
xorl %r15d, %r15d
jmp 0x2a86
movq 0x8(%rsp), %r14
callq 0x2040
movl (%rax), %edi
callq 0x20e0
leaq 0x9607(%rip), %rdi      # 0xc07c
movq %r14, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2662
pushq $-0x1
popq %r15
movq 0x8(%rsp), %rdi
callq 0x22e0
andq $0x0, 0x8(%rsp)
movl %ebx, %edi
callq 0x2410
leaq 0x10(%rsp), %rdi
callq 0x423c
movl %r15d, %eax
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9562(%rip), %rdi      # 0xc020
xorl %eax, %eax
callq 0x25ca
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x423c
movq %rbx, %rdi
callq 0x2400