// File: /workspace/github_cpp_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testq %rdi, %rdi
je 0x2c60
movq %rdi, %r12
cmpb $0x0, (%rdi)
je 0x2c60
movl %edx, %ebp
movl %esi, %r14d
xorl %edi, %edi
callq 0x2410
movl %eax, %ebx
movq %r12, %rdi
testb %r14b, %r14b
je 0x2af0
callq 0x2140
movq %rax, %r13
movq %rax, %r15
shlq $0x20, %r15
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq %r15, %rax
sarq $0x20, %rax
cmpb $0x2f, (%r12,%rax)
jne 0x2b0e
movq %r12, %rdi
xorl %esi, %esi
callq 0x2906
movq %rax, %r14
jmp 0x2b64
movl %ebp, %esi
callq 0x2150
movl %eax, %r15d
testl %eax, %eax
jne 0x2c1f
movl %ebx, %edi
callq 0x2410
jmp 0x2c60
movabsq $0x200000000, %rax      # imm = 0x200000000
addq %rax, %r15
je 0x2c2c
sarq $0x20, %r15
movq %r15, %rdi
callq 0x2330
testq %rax, %rax
je 0x2c2c
movq %rax, %r14
movq %rax, %rdi
movq %r12, %rsi
callq 0x2220
movq %r14, %rdi
callq 0x2140
movw $0x2f, (%r14,%rax)
shlq $0x20, %r13
movabsq $0x100000000, %r15      # imm = 0x100000000
addq %r13, %r15
leaq 0x111b5(%rip), %r13     # 0x13d20
movq %r13, 0x8(%rsp)
movb $0x0, 0x10(%rsp)
movabsq $0x400000000, %rax      # imm = 0x400000000
movq %rax, 0x20(%rsp)
movl $0x20, %edi
callq 0x2330
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x2c2c
sarq $0x20, %r15
leaq (%r14,%r15), %rax
decq %rax
movq %rax, %r15
cmpq %r14, %rax
jbe 0x2bba
leaq -0x1(%r15), %rax
cmpb $0x2f, -0x1(%r15)
je 0x2ba7
movb $0x0, (%r15)
movq %r14, %rdi
callq 0x2ce6
testl %eax, %eax
jne 0x2c72
leaq 0x8(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x450e
decq %r15
cmpq %r14, %r15
jbe 0x2c76
leaq -0x1(%r15), %rax
cmpb $0x2f, (%r15)
movq %rax, %r15
jne 0x2be1
incq %rax
movq %rax, %r15
cmpq %r14, %rax
jbe 0x2c0d
leaq -0x1(%r15), %rax
cmpb $0x2f, -0x1(%r15)
je 0x2bfa
movb $0x0, (%r15)
movq %r14, %rdi
callq 0x2ce6
testl %eax, %eax
je 0x2bd3
jmp 0x2c72
movq %r12, %rdi
callq 0x2512
jmp 0x2b02
leaq 0xb3ed(%rip), %rdi      # 0xe020
xorl %eax, %eax
callq 0x286c
movq %rax, %rbx
movq %r13, 0x8(%rsp)
cmpb $0x0, 0x10(%rsp)
jne 0x2c58
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x2c58
callq 0x22e0
movq %rbx, %rdi
callq 0x2400
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x2f, (%r15)
movl 0x20(%rsp), %eax
testl %eax, %eax
je 0x2caf
decl %eax
movl %eax, 0x20(%rsp)
movq 0x18(%rsp), %rcx
movq (%rcx,%rax,8), %r15
movq %r14, %rdi
movl %ebp, %esi
callq 0x2150
testl %eax, %eax
je 0x2c72
movq %r14, %rdi
callq 0x2537
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testb $0x1, %al
je 0x2cb7
jmp 0x2cbf
xorl %r15d, %r15d
testq %r14, %r14
je 0x2cbf
movq %r14, %rdi
callq 0x22e0
movl %ebx, %edi
callq 0x2410
movq %r13, 0x8(%rsp)
cmpb $0x0, 0x10(%rsp)
jne 0x2c60
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x2c60
callq 0x22e0
jmp 0x2c60