// File: /workspace/github_c_makefile_star2_O3/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, 0x10(%rsp)
callq 0x3540
movq %rax, 0x8(%rsp)
movzbl (%rbx), %eax
leaq 0x470b(%rip), %rdx      # 0x8895
testl %eax, %eax
je 0x41a0
cmpl $0x2f, %eax
jne 0x4199
cmpb $0x0, 0x1(%rbx)
je 0x41a0
leaq 0x3fe9(%rip), %rdx      # 0x8189
movq %rbx, 0x20(%rsp)
xorl %r13d, %r13d
movabsq $0x100000000, %r14      # imm = 0x100000000
movl $0x1, %r9d
xorl %ebp, %ebp
xorl %ecx, %ecx
xorl %ebx, %ebx
xorl %r15d, %r15d
movzbl (%r12,%rbp), %esi
testl %esi, %esi
je 0x4217
cmpl $0x2f, %esi
je 0x4217
leaq (%r12,%rbp), %rdi
testq %r15, %r15
movq %r15, %r8
cmoveq %rdi, %r8
cmpb $0x7b, %sil
cmoveq %r8, %r15
testq %rbx, %rbx
cmovneq %rbx, %rdi
cmpb $0x7d, %sil
cmoveq %rdi, %rbx
leal -0x5b(%rsi), %edi
cmpb $0x2, %dil
cmovbl %r9d, %ecx
cmpb $0x2a, %sil
cmovel %r9d, %ecx
cmpb $0x3f, %sil
cmovel %r9d, %ecx
incq %rbp
addq %r14, %r13
jmp 0x41c1
testq %r15, %r15
je 0x4249
testq %rbx, %rbx
je 0x42d5
movq %r12, %rdi
callq 0x3190
incl %eax
cmpl $0xc9, %eax
movq %rax, 0x18(%rsp)
jge 0x42f1
leaq 0x40(%rsp), %rbp
jmp 0x430c
testl %ecx, %ecx
je 0x43bc
movq %rdx, 0x18(%rsp)
testb %al, %al
leaq 0x3f70(%rip), %r14      # 0x81cf
movq 0x20(%rsp), %r15
cmovneq %r15, %r14
leaq 0x110(%rsp), %rsi
movq %r14, %rdi
callq 0x3380
xorl %ebx, %ebx
testl %eax, %eax
jne 0x4477
movl $0xf000, %eax           # imm = 0xF000
andl 0x128(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x4477
movq %r14, %rdi
callq 0x3170
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x448b
movq %r15, %rdi
callq 0x3190
movq %rax, 0x38(%rsp)
cmpl $0xc8, %ebp
jge 0x44c6
leaq 0x1a0(%rsp), %rdi
jmp 0x44e2
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x3270
leaq 0x3ec4(%rip), %rax      # 0x81ad
movq %rax, (%rbx)
jmp 0x4384
movl %eax, %esi
leaq 0x3e3f(%rip), %rdx      # 0x8139
movq 0x8(%rsp), %rdi
movl $0xf0, %ecx
callq 0x34c0
movq %rax, %rbp
movl %r15d, %eax
subl %r12d, %eax
movslq %eax, %r13
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x30a0
addq %rbp, %r13
incq %rbx
cmpb $0x7d, (%r15)
je 0x438e
movq %r15, %rsi
incq %rsi
xorl %r12d, %r12d
movq %rsi, %r15
movzbl (%r15), %eax
cmpl $0x2c, %eax
je 0x4351
cmpl $0x7d, %eax
je 0x4351
incq %r15
addq %r14, %r12
jmp 0x433b
sarq $0x20, %r12
movq %r13, %rdi
movq %r12, %rdx
callq 0x30a0
addq %r13, %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x30e0
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rbp, %rdx
callq 0x415a
testl %eax, %eax
je 0x4329
movl $0x1, %ebx
jmp 0x4477
xorl %ebx, %ebx
cmpl $0xc9, 0x18(%rsp)
jl 0x4477
leaq 0x3d94(%rip), %rdx      # 0x8139
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
movl $0x102, %ecx            # imm = 0x102
callq 0x31f0
jmp 0x4477
testb %sil, %sil
movq 0x20(%rsp), %r15
je 0x4462
movq %rdx, %r13
movq %r15, %rdi
callq 0x3190
addq %rbp, %rax
addq $0x2, %rax
leaq 0x40(%rsp), %r14
movq %r14, %rbx
cmpl $0xc9, %eax
jl 0x4406
movl %eax, %esi
leaq 0x3d45(%rip), %rdx      # 0x8139
movq 0x8(%rsp), %rdi
movl $0x171, %ecx            # imm = 0x171
callq 0x34c0
movq %rax, %rbx
leaq 0x3de9(%rip), %rsi      # 0x81f6
movq %rbx, %rdi
movq %r15, %rdx
movq %r13, %rcx
movl %ebp, %r8d
movq %r12, %r9
xorl %eax, %eax
callq 0x35a0
leaq (%r12,%rbp), %rdx
incq %rdx
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x415a
movl %eax, %ebp
cmpq %r14, %rbx
je 0x4457
leaq 0x3cf4(%rip), %rdx      # 0x8139
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movl $0x176, %ecx            # imm = 0x176
callq 0x31f0
movl $0x1, %ebx
testl %ebp, %ebp
jne 0x4477
jmp 0x4475
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rcx
movl %ebp, %r8d
callq 0x4648
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x3270
movq %rbx, %rdi
callq 0x31b0
leaq 0x3d2a(%rip), %rsi      # 0x81d1
leaq 0x3d3d(%rip), %rcx      # 0x81eb
movq %rbx, %rdi
movq %r14, %rdx
movq %rax, %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x3510
jmp 0x4384
leal 0x1(%rbp), %esi
leaq 0x3c69(%rip), %rdx      # 0x8139
movq 0x8(%rsp), %rdi
movl $0x12e, %ecx            # imm = 0x12E
callq 0x34c0
movq %rax, %rdi
sarq $0x20, %r13
movq %rdi, %rbx
movq %r12, %rsi
movq %r13, %rdx
callq 0x30a0
movq %rbx, %r14
movb $0x0, (%rbx,%r13)
movq 0x28(%rsp), %r13
movq %r13, %rdi
callq 0x33f0
testq %rax, %rax
je 0x4614
movq %rax, %rbx
leaq (%r12,%rbp), %rax
incq %rax
movq %rax, 0x30(%rsp)
cmpb $0x2e, 0x13(%rbx)
jne 0x4531
cmpb $0x2e, (%r14)
jne 0x4600
addq $0x13, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3440
testl %eax, %eax
je 0x4600
movq %rbx, %rdi
callq 0x3190
cmpb $0x0, (%r12,%rbp)
je 0x45e8
addl 0x38(%rsp), %eax
leaq 0x40(%rsp), %r13
cmpl $0xc7, %eax
jl 0x458a
addl $0x2, %eax
movq 0x8(%rsp), %rdi
movq %rax, %rsi
leaq 0x3bbc(%rip), %rdx      # 0x8139
movl $0x14b, %ecx            # imm = 0x14B
callq 0x34c0
movq %rax, %r13
movq %r13, %rdi
leaq 0x3c5b(%rip), %rsi      # 0x81ef
movq %r15, %rdx
movq 0x18(%rsp), %rcx
movq %rbx, %r8
xorl %eax, %eax
callq 0x35a0
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movq 0x30(%rsp), %rdx
callq 0x415a
movl %eax, %ebx
leaq 0x40(%rsp), %rax
cmpq %rax, %r13
je 0x45dd
movq 0x8(%rsp), %rdi
movq %r13, %rsi
leaq 0x3b66(%rip), %rdx      # 0x8139
movl $0x150, %ecx            # imm = 0x150
callq 0x31f0
testl %ebx, %ebx
movq 0x28(%rsp), %r13
je 0x4600
jmp 0x4616
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rdx
movq %rbx, %rcx
movl %eax, %r8d
callq 0x4648
movq %r13, %rdi
callq 0x33f0
movq %rax, %rbx
testq %rax, %rax
jne 0x4521
xorl %ebx, %ebx
movq %r13, %rdi
callq 0x32c0
leaq 0x1a0(%rsp), %rax
cmpq %rax, %r14
je 0x4477
leaq 0x3b03(%rip), %rdx      # 0x8139
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movl $0x15a, %ecx            # imm = 0x15A
jmp 0x43b2