// File: /workspace/github_cpp_makefile_star2_O1/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, %r14d      # imm = 0xFFFFFFFF
testq %rdi, %rdi
je 0x2ce6
movq %rdi, %r15
cmpb $0x0, (%rdi)
je 0x2ce6
movl %edx, %ebp
movl %esi, %r12d
xorl %edi, %edi
callq 0x2410
movl %eax, %ebx
movq %r15, %rdi
testb %r12b, %r12b
je 0x2b29
callq 0x2140
movq %rax, %r12
movq %rax, %rdi
shlq $0x20, %rdi
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq %rdi, %rax
sarq $0x20, %rax
cmpb $0x2f, (%r15,%rax)
jne 0x2b47
movq %r15, %rdi
xorl %esi, %esi
callq 0x2936
movq %rax, %r14
jmp 0x2b8d
movl %ebp, %esi
callq 0x2150
movl %eax, %r14d
testl %eax, %eax
jne 0x2cf8
movl %ebx, %edi
callq 0x2410
jmp 0x2ce6
movabsq $0x200000000, %rax      # imm = 0x200000000
addq %rax, %rdi
je 0x2b64
sarq $0x20, %rdi
callq 0x2330
movq %rax, %r14
jmp 0x2b67
xorl %r14d, %r14d
testq %r14, %r14
je 0x2d05
movq %r14, %rdi
movq %r15, %rsi
callq 0x2220
movq %r14, %rdi
callq 0x2140
movw $0x2f, (%r14,%rax)
incq %r12
leaq 0x1118c(%rip), %r13     # 0x13d20
movq %r13, 0x8(%rsp)
movb $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movabsq $0x400000000, %rax      # imm = 0x400000000
movq %rax, 0x20(%rsp)
movl $0x20, %edi
callq 0x2330
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x2d05
movslq %r12d, %rax
addq %r14, %rax
decq %rax
movq %rax, %r12
cmpq %r14, %rax
jbe 0x2bec
leaq -0x1(%r12), %rax
cmpb $0x2f, -0x1(%r12)
je 0x2bd7
movb $0x0, (%r12)
movq %r14, %rdi
callq 0x2d34
testl %eax, %eax
jne 0x2c56
leaq 0x8(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x458a
decq %r12
cmpq %r14, %r12
jbe 0x2c53
movq %r12, %rax
decq %r12
cmpb $0x2f, (%rax)
jne 0x2c10
cmpq %r14, %rax
jbe 0x2c53
incq %r12
movq %r12, %rax
movq %rax, %r12
cmpq %r14, %rax
jbe 0x2c40
leaq -0x1(%r12), %rax
cmpb $0x2f, -0x1(%r12)
je 0x2c2b
movb $0x0, (%r12)
movq %r14, %rdi
callq 0x2d34
testl %eax, %eax
je 0x2c02
jmp 0x2c56
xorl %r12d, %r12d
testq %r12, %r12
je 0x2c60
movb $0x2f, (%r12)
movl 0x20(%rsp), %eax
testl %eax, %eax
je 0x2ca3
decl %eax
movl %eax, 0x20(%rsp)
movq 0x18(%rsp), %rcx
movq (%rcx,%rax,8), %r12
movq %r14, %rdi
movl %ebp, %esi
callq 0x2150
movl %eax, %r15d
testl %eax, %eax
jne 0x2c94
movb $0x2f, (%r12)
testl %r15d, %r15d
je 0x2c60
jmp 0x2cbc
movq %r14, %rdi
movl %ebx, %esi
callq 0x254e
xorl %r14d, %r14d
jmp 0x2c8d
testq %r14, %r14
je 0x2cb0
movq %r14, %rdi
callq 0x22e0
movl %ebx, %edi
callq 0x2410
xorl %r14d, %r14d
jmp 0x2cc2
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq %r13, 0x8(%rsp)
cmpb $0x0, 0x10(%rsp)
jne 0x2ce6
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x2cdd
callq 0x22e0
movq $0x0, 0x18(%rsp)
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x2512
jmp 0x2b3b
leaq 0xb314(%rip), %rdi      # 0xe020
xorl %eax, %eax
callq 0x289c
movq %rax, %rbx
movq %r13, 0x8(%rsp)
cmpb $0x0, 0x10(%rsp)
jne 0x2d2c
leaq 0x18(%rsp), %rdi
callq 0x2537
movq %rbx, %rdi
callq 0x2400