// File: /workspace/github_c_makefile_star2_O2/haxlang[P]hax/haxGlob.c
// Function: DoGlob
static int
DoGlob(
    Hax_Interp *interp,			/* Interpreter to use for error
					 * reporting (e.g. unmatched brace). */
    char *dir,				/* Name of a directory at which to
					 * start glob expansion.  This name
					 * is fixed: it doesn't contain any
					 * globbing chars. */
    char *rem				/* Path to glob-expand. */)
{
    /*
     * When this procedure is entered, the name to be globbed may
     * already have been partly expanded by ancestor invocations of
     * DoGlob.  The part that's already been expanded is in "dir"
     * (this may initially be empty), and the part still to expand
     * is in "rem".  This procedure expands "rem" one level, making
     * recursive calls to itself if there's still more stuff left
     * in the remainder.
     */

    Hax_Memoryp *memoryp;
    char *p;
    char c;
    char *openBrace, *closeBrace;
    int gotSpecial, result;
    const char *separator;

    memoryp = Hax_GetMemoryp (interp);

    /*
     * Figure out whether we'll need to add a slash between the directory
     * name and file names within the directory when concatenating them
     * together.
     */

    if ((dir[0] == 0) || ((dir[0] == '/') && (dir[1] == 0))) {
	separator = "";
    } else {
	separator = "/";
    }

    /*
     * When generating information for the next lower call,
     * use static areas if the name is short, and malloc if the name
     * is longer.
     */

#define STATIC_SIZE 200

    /*
     * First, find the end of the next element in rem, checking
     * along the way for special globbing characters.
     */

    gotSpecial = 0;
    openBrace = closeBrace = NULL;
    for (p = rem; ; p++) {
	c = *p;
	if ((c == '\0') || (c == '/')) {
	    break;
	}
	if ((c == '{') && (openBrace == NULL)) {
	    openBrace = p;
	}
	if ((c == '}') && (closeBrace == NULL)) {
	    closeBrace = p;
	}
	if ((c == '*') || (c == '[') || (c == '\\') || (c == '?')) {
	    gotSpecial = 1;
	}
    }

    /*
     * If there is an open brace in the argument, then make a recursive
     * call for each element between the braces.  In this case, the
     * recursive call to DoGlob uses the same "dir" that we got.
     * If there are several brace-pairs in a single name, we just handle
     * one here, and the others will be handled in recursive calls.
     */

    if (openBrace != NULL) {
	int remLength, l1, l2;
	char static1[STATIC_SIZE];
	char *element, *newRem;

	if (closeBrace == NULL) {
	    Hax_ResetResult(interp);
	    interp->result = (char *) "unmatched open-brace in file name";
	    return HAX_ERROR;
	}
	remLength = strlen(rem) + 1;
	if (remLength <= STATIC_SIZE) {
	    newRem = static1;
	} else {
	    newRem = (char *) ckalloc(memoryp, (unsigned) remLength);
	}
	l1 = openBrace-rem;
	strncpy(newRem, rem, l1);
	p = openBrace;
	for (p = openBrace; *p != '}'; ) {
	    element = p+1;
	    for (p = element; ((*p != '}') && (*p != ',')); p++) {
		/* Empty loop body:  just find end of this element. */
	    }
	    l2 = p - element;
	    strncpy(newRem+l1, element, l2);
	    strcpy(newRem+l1+l2, closeBrace+1);
	    if (DoGlob(interp, dir, newRem) != HAX_OK) {
		return HAX_ERROR;
	    }
	}
	if (remLength > STATIC_SIZE) {
	    ckfree(memoryp, newRem);
	}
	return HAX_OK;
    }

    /*
     * If there were any pattern-matching characters, then scan through
     * the directory to find all the matching names.
     */

    if (gotSpecial) {
	DIR *d;
	struct dirent *entryPtr;
	int l1, l2;
	char *pattern, *newDir, *dirName;
	char static1[STATIC_SIZE], static2[STATIC_SIZE];
	struct stat statBuf;

	/*
	 * Be careful not to do any actual file system operations on a
	 * directory named "";  instead, use ".".  This is needed because
	 * some versions of UNIX don't treat "" like "." automatically.
	 */

	if (*dir == '\0') {
	    dirName = (char *) ".";
	} else {
	    dirName = dir;
	}
	if ((stat(dirName, &statBuf) != 0) || !S_ISDIR(statBuf.st_mode)) {
	    return HAX_OK;
	}
	d = opendir(dirName);
	if (d == NULL) {
	    Hax_ResetResult(interp);
	    Hax_AppendResult(interp, "couldn't read directory \"",
		    dirName, "\": ", Hax_UnixError(interp), (char *) NULL);
	    return HAX_ERROR;
	}
	l1 = strlen(dir);
	l2 = (p - rem);
	if (l2 < STATIC_SIZE) {
	    pattern = static2;
	} else {
	    pattern = (char *) ckalloc(memoryp, (unsigned) (l2+1));
	}
	strncpy(pattern, rem, l2);
	pattern[l2] = '\0';
	result = HAX_OK;
	while (1) {
	    entryPtr = readdir(d);
	    if (entryPtr == NULL) {
		break;
	    }

	    /*
	     * Don't match names starting with "." unless the "." is
	     * present in the pattern.
	     */

	    if ((*entryPtr->d_name == '.') && (*pattern != '.')) {
		continue;
	    }
	    if (Hax_StringMatch(entryPtr->d_name, pattern)) {
		int nameLength = strlen(entryPtr->d_name);
		if (*p == 0) {
		    AppendResult(interp, dir, (char *) separator, entryPtr->d_name,
			    nameLength);
		} else {
		    if ((l1+nameLength+2) <= STATIC_SIZE) {
			newDir = static1;
		    } else {
			newDir = (char *) ckalloc(memoryp, (unsigned)
				(l1+nameLength+2));
		    }
		    sprintf(newDir, "%s%s%s", dir, separator, entryPtr->d_name);
		    result = DoGlob(interp, newDir, p+1);
		    if (newDir != static1) {
			ckfree(memoryp, newDir);
		    }
		    if (result != HAX_OK) {
			break;
		    }
		}
	    }
	}
	closedir(d);
	if (pattern != static2) {
	    ckfree(memoryp, pattern);
	}
	return result;
    }

    /*
     * This is the simplest case:  just another path element.  Move
     * it to the dir side and recurse (or just add the name to the
     * list, if we're at the end of the path).
     */

    if (*p == 0) {
	AppendResult(interp, dir, (char *) separator, rem, p-rem);
    } else {
	int l1, l2;
	char *newDir;
	char static1[STATIC_SIZE];

	l1 = strlen(dir);
	l2 = l1 + (p - rem) + 2;
	if (l2 <= STATIC_SIZE) {
	    newDir = static1;
	} else {
	    newDir = (char *) ckalloc(memoryp, (unsigned) l2);
	}
	sprintf(newDir, "%s%s%.*s", dir, separator, (int)(p-rem), rem);
	result = DoGlob(interp, newDir, p+1);
	if (newDir != static1) {
	    ckfree(memoryp, newDir);
	}
	if (result != HAX_OK) {
	    return HAX_ERROR;
	}
    }
    return HAX_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, 0x8(%rsp)
callq 0x3530
movq %rax, (%rsp)
movzbl (%rbx), %eax
leaq 0x4774(%rip), %rdx      # 0x8895
testl %eax, %eax
je 0x4137
cmpl $0x2f, %eax
jne 0x4130
cmpb $0x0, 0x1(%rbx)
je 0x4137
leaq 0x4052(%rip), %rdx      # 0x8189
movq %rbx, 0x28(%rsp)
xorl %r14d, %r14d
movabsq $0x100000000, %r15      # imm = 0x100000000
pushq $0x1
popq %r8
xorl %ebx, %ebx
xorl %ecx, %ecx
xorl %r13d, %r13d
xorl %ebp, %ebp
movzbl (%r12,%rbx), %r9d
testl %r9d, %r9d
je 0x41af
cmpl $0x2f, %r9d
je 0x41af
leaq (%r12,%rbx), %rsi
testq %rbp, %rbp
movq %rbp, %rdi
cmoveq %rsi, %rdi
cmpb $0x7b, %r9b
cmoveq %rdi, %rbp
testq %r13, %r13
cmovneq %r13, %rsi
cmpb $0x7d, %r9b
cmoveq %rsi, %r13
leal -0x5b(%r9), %esi
cmpb $0x2, %sil
cmovbl %r8d, %ecx
cmpb $0x2a, %r9b
cmovel %r8d, %ecx
cmpb $0x3f, %r9b
cmovel %r8d, %ecx
incq %rbx
addq %r15, %r14
jmp 0x4156
testq %rbp, %rbp
je 0x41e1
testq %r13, %r13
je 0x426d
movq %r12, %rdi
callq 0x3190
incl %eax
cmpl $0xc9, %eax
movq %rax, 0x18(%rsp)
jge 0x4289
leaq 0x40(%rsp), %r14
jmp 0x42a3
testl %ecx, %ecx
je 0x434c
movq %rdx, 0x18(%rsp)
testb %al, %al
leaq 0x3fd8(%rip), %r13      # 0x81cf
movq 0x28(%rsp), %rbp
cmovneq %rbp, %r13
leaq 0x110(%rsp), %rsi
movq %r13, %rdi
callq 0x3370
xorl %ecx, %ecx
testl %eax, %eax
jne 0x443b
movl $0xf000, %eax           # imm = 0xF000
andl 0x128(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x443b
movq %r13, %rdi
callq 0x3170
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x4402
movq %rbp, %rdi
callq 0x3190
movq %rax, 0x38(%rsp)
cmpl $0xc8, %ebx
jge 0x444f
leaq 0x1a0(%rsp), %r15
jmp 0x446a
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x3260
leaq 0x3f2c(%rip), %rax      # 0x81ad
movq %rax, (%rbx)
jmp 0x4438
movl %eax, %esi
leaq 0x3ea7(%rip), %rdx      # 0x8139
movq (%rsp), %rdi
movl $0xf0, %ecx
callq 0x34b0
movq %rax, %r14
movl %ebp, %eax
subl %r12d, %eax
movslq %eax, %rbx
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x30a0
addq %r14, %rbx
incq %r13
cmpb $0x7d, (%rbp)
je 0x431f
movq %rbp, %rsi
incq %rsi
xorl %r12d, %r12d
movq %rsi, %rbp
movzbl (%rbp), %eax
cmpl $0x2c, %eax
je 0x42e7
cmpl $0x7d, %eax
je 0x42e7
incq %rbp
addq %r15, %r12
jmp 0x42d1
sarq $0x20, %r12
movq %rbx, %rdi
movq %r12, %rdx
callq 0x30a0
addq %rbx, %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x30e0
movq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %r14, %rdx
callq 0x40f2
testl %eax, %eax
je 0x42bf
jmp 0x4438
xorl %ecx, %ecx
cmpl $0xc9, 0x18(%rsp)
jl 0x443b
leaq 0x3e03(%rip), %rdx      # 0x8139
movq (%rsp), %rdi
movq %r14, %rsi
movl $0x102, %ecx            # imm = 0x102
callq 0x31f0
jmp 0x43fe
testb %r9b, %r9b
movq 0x28(%rsp), %r13
je 0x43eb
movq %rdx, %rbp
movq %r13, %rdi
callq 0x3190
addq %rbx, %rax
addq $0x2, %rax
leaq 0x40(%rsp), %r15
movq %r15, %r14
cmpl $0xc9, %eax
jl 0x4395
movl %eax, %esi
leaq 0x3db5(%rip), %rdx      # 0x8139
movq (%rsp), %rdi
movl $0x171, %ecx            # imm = 0x171
callq 0x34b0
movq %rax, %r14
leaq 0x3e5a(%rip), %rsi      # 0x81f6
movq %r14, %rdi
movq %r13, %rdx
movq %rbp, %rcx
movl %ebx, %r8d
movq %r12, %r9
xorl %eax, %eax
callq 0x3590
leaq (%r12,%rbx), %rdx
incq %rdx
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x40f2
movl %eax, %ebx
cmpq %r15, %r14
je 0x43e5
leaq 0x3d65(%rip), %rdx      # 0x8139
movq (%rsp), %rdi
movq %r14, %rsi
movl $0x176, %ecx            # imm = 0x176
callq 0x31f0
testl %ebx, %ebx
jne 0x4438
jmp 0x43fe
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rcx
movl %ebx, %r8d
callq 0x45c9
xorl %ecx, %ecx
jmp 0x443b
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x3260
movq %rbx, %rdi
callq 0x31b0
leaq 0x3db3(%rip), %rsi      # 0x81d1
leaq 0x3dc6(%rip), %rcx      # 0x81eb
movq %rbx, %rdi
movq %r13, %rdx
movq %rax, %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x3500
pushq $0x1
popq %rcx
movl %ecx, %eax
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leal 0x1(%rbx), %esi
leaq 0x3ce0(%rip), %rdx      # 0x8139
movq (%rsp), %rdi
movl $0x12e, %ecx            # imm = 0x12E
callq 0x34b0
movq %rax, %r15
movq 0x20(%rsp), %r13
sarq $0x20, %r14
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x30a0
movb $0x0, (%r15,%r14)
leaq (%r12,%rbx), %rax
incq %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x14(%rsp)
movq %r13, %rdi
callq 0x33e0
testq %rax, %rax
je 0x4593
movq %rax, %r14
cmpb $0x2e, 0x13(%rax)
jne 0x44ba
cmpb $0x2e, (%r15)
jne 0x449a
addq $0x13, %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x3430
testl %eax, %eax
je 0x449a
movq %r14, %rdi
callq 0x3190
cmpb $0x0, (%r12,%rbx)
je 0x4571
addl 0x38(%rsp), %eax
leaq 0x40(%rsp), %r13
cmpl $0xc7, %eax
jl 0x450e
addl $0x2, %eax
movq (%rsp), %rdi
movq %rax, %rsi
leaq 0x3c38(%rip), %rdx      # 0x8139
movl $0x14b, %ecx            # imm = 0x14B
callq 0x34b0
movq %rax, %r13
movq %r13, %rdi
leaq 0x3cd7(%rip), %rsi      # 0x81ef
movq %rbp, %rdx
movq 0x18(%rsp), %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x3590
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movq 0x30(%rsp), %rdx
callq 0x40f2
movl %eax, %r14d
leaq 0x40(%rsp), %rax
cmpq %rax, %r13
je 0x4561
movq (%rsp), %rdi
movq %r13, %rsi
leaq 0x3be2(%rip), %rdx      # 0x8139
movl $0x150, %ecx            # imm = 0x150
callq 0x31f0
testl %r14d, %r14d
movq 0x20(%rsp), %r13
je 0x449a
jmp 0x458e
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %rdx
movq %r14, %rcx
movl %eax, %r8d
callq 0x45c9
jmp 0x449a
movl %r14d, 0x14(%rsp)
movq %r13, %rdi
callq 0x32b0
leaq 0x1a0(%rsp), %rax
cmpq %rax, %r15
je 0x45c0
leaq 0x3b8a(%rip), %rdx      # 0x8139
movq (%rsp), %rdi
movq %r15, %rsi
movl $0x15a, %ecx            # imm = 0x15A
callq 0x31f0
movl 0x14(%rsp), %ecx
jmp 0x443b