// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movb %sil, %al
movq %rdi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movl %edx, -0x18(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x2afd
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x2b09
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2e3b
xorl %edi, %edi
callq 0x2420
movl %eax, -0x1c(%rbp)
testb $0x1, -0x11(%rbp)
jne 0x2b6a
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x29b0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x2b57
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
callq 0x2040
movl (%rax), %edi
callq 0x20e0
movq -0x80(%rbp), %rsi
movq %rax, %rdx
leaq 0x1150e(%rip), %rdi     # 0x1405e
movb $0x0, %al
callq 0x26f0
movl -0x1c(%rbp), %edi
callq 0x2420
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2e3b
movq -0x10(%rbp), %rdi
callq 0x2140
movl %eax, -0x24(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2f, %eax
jne 0x2ba5
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x27b0
movq %rax, -0x30(%rbp)
jmp 0x2bef
movl -0x24(%rbp), %eax
addl $0x2, %eax
movslq %eax, %rsi
leaq -0x30(%rbp), %rdi
callq 0x58f0
testb $0x1, %al
jne 0x2bc9
leaq 0x11461(%rip), %rdi     # 0x14023
movb $0x0, %al
callq 0x2640
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x2240
movq -0x30(%rbp), %rdi
leaq 0x1149e(%rip), %rsi     # 0x1407f
callq 0x2410
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movq -0x30(%rbp), %rax
movslq -0x24(%rbp), %rcx
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
leaq -0x58(%rbp), %rdi
movl $0x4, %esi
callq 0x5940
movq -0x38(%rbp), %rcx
xorl %eax, %eax
cmpq -0x30(%rbp), %rcx
movb %al, -0x81(%rbp)
jbe 0x2c36
movq -0x38(%rbp), %rax
movsbl -0x1(%rax), %eax
cmpl $0x2f, %eax
sete %al
movb %al, -0x81(%rbp)
movb -0x81(%rbp), %al
testb $0x1, %al
jne 0x2c42
jmp 0x2c50
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x2c10
movq -0x38(%rbp), %rax
movb $0x0, (%rax)
movl $0x0, -0x5c(%rbp)
movq -0x30(%rbp), %rdi
callq 0x2e50
movl %eax, -0x88(%rbp)
jmp 0x2c6f
movl -0x88(%rbp), %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, %eax
jne 0x2d49
movq -0x38(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x59b0
jmp 0x2c90
jmp 0x2c92
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rcx
xorl %eax, %eax
cmpq -0x30(%rbp), %rcx
movb %al, -0x89(%rbp)
jbe 0x2cc3
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
setne %al
movb %al, -0x89(%rbp)
movb -0x89(%rbp), %al
testb $0x1, %al
jne 0x2c92
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
ja 0x2cfb
movq $0x0, -0x38(%rbp)
jmp 0x2d49
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x5af0
jmp 0x2e47
jmp 0x2cfd
movq -0x38(%rbp), %rcx
xorl %eax, %eax
cmpq -0x30(%rbp), %rcx
movb %al, -0x8a(%rbp)
jbe 0x2d23
movq -0x38(%rbp), %rax
movsbl -0x1(%rax), %eax
cmpl $0x2f, %eax
sete %al
movb %al, -0x8a(%rbp)
movb -0x8a(%rbp), %al
testb $0x1, %al
jne 0x2d2f
jmp 0x2d3d
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x2cfd
movq -0x38(%rbp), %rax
movb $0x0, (%rax)
jmp 0x2c5e
cmpq $0x0, -0x38(%rbp)
je 0x2d57
movq -0x38(%rbp), %rax
movb $0x2f, (%rax)
jmp 0x2d59
leaq -0x58(%rbp), %rdi
callq 0x5a20
movl %eax, -0x90(%rbp)
jmp 0x2d6a
movl -0x90(%rbp), %eax
cmpl $0x0, %eax
jbe 0x2e11
leaq -0x58(%rbp), %rdi
callq 0x5a40
movq %rax, -0x98(%rbp)
jmp 0x2d8b
movq -0x98(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x70(%rbp)
movq -0x30(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x29b0
movl %eax, -0x70(%rbp)
cmpl $0x0, %eax
je 0x2e05
movq -0x30(%rbp), %rax
movq %rax, -0xa0(%rbp)
callq 0x2040
movl (%rax), %edi
callq 0x20e0
movq -0xa0(%rbp), %rsi
movq %rax, %rdx
leaq 0x112a8(%rip), %rdi     # 0x14081
xorl %eax, %eax
callq 0x26f0
jmp 0x2de2
leaq -0x30(%rbp), %rdi
callq 0x5a90
jmp 0x2ded
movl -0x1c(%rbp), %edi
callq 0x2420
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x74(%rbp)
jmp 0x2e32
movq -0x38(%rbp), %rax
movb $0x2f, (%rax)
jmp 0x2d59
leaq -0x30(%rbp), %rdi
callq 0x5a90
jmp 0x2e1c
movl -0x1c(%rbp), %edi
callq 0x2420
movl $0x0, -0x4(%rbp)
movl $0x1, -0x74(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x5af0
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x2400