// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x4a0, %rsp            # imm = 0x4A0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3540
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x46a9
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
jne 0x46b6
movq -0x18(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x0, %eax
jne 0x46b6
leaq 0x7158(%rip), %rax      # 0xb808
movq %rax, -0x58(%rbp)
jmp 0x46c1
leaq 0x6acc(%rip), %rax      # 0xb189
movq %rax, -0x58(%rbp)
movl $0x0, -0x4c(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movb (%rax), %al
movb %al, -0x31(%rbp)
movsbl -0x31(%rbp), %eax
cmpl $0x0, %eax
je 0x46fb
movsbl -0x31(%rbp), %eax
cmpl $0x2f, %eax
jne 0x46fd
jmp 0x476b
movsbl -0x31(%rbp), %eax
cmpl $0x7b, %eax
jne 0x4715
cmpq $0x0, -0x40(%rbp)
jne 0x4715
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movsbl -0x31(%rbp), %eax
cmpl $0x7d, %eax
jne 0x472d
cmpq $0x0, -0x48(%rbp)
jne 0x472d
movq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movsbl -0x31(%rbp), %eax
cmpl $0x2a, %eax
je 0x4751
movsbl -0x31(%rbp), %eax
cmpl $0x5b, %eax
je 0x4751
movsbl -0x31(%rbp), %eax
cmpl $0x5c, %eax
je 0x4751
movsbl -0x31(%rbp), %eax
cmpl $0x3f, %eax
jne 0x4758
movl $0x1, -0x4c(%rbp)
jmp 0x475a
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x46e0
cmpq $0x0, -0x40(%rbp)
je 0x4935
cmpq $0x0, -0x48(%rbp)
jne 0x47a0
movq -0x10(%rbp), %rdi
callq 0x3270
movq -0x10(%rbp), %rax
leaq 0x6a1c(%rip), %rcx      # 0xb1ad
movq %rcx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x4dae
movq -0x20(%rbp), %rdi
callq 0x3190
addq $0x1, %rax
movl %eax, -0x5c(%rbp)
cmpl $0xc8, -0x5c(%rbp)
jg 0x47c9
leaq -0x130(%rbp), %rax
movq %rax, -0x140(%rbp)
jmp 0x47ea
movq -0x28(%rbp), %rdi
movl -0x5c(%rbp), %eax
movl %eax, %esi
leaq 0x6960(%rip), %rdx      # 0xb139
movl $0xf0, %ecx
callq 0x34c0
movq %rax, -0x140(%rbp)
movq -0x40(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x60(%rbp)
movq -0x140(%rbp), %rdi
movq -0x20(%rbp), %rsi
movslq -0x60(%rbp), %rdx
callq 0x30a0
movq -0x40(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x7d, %eax
je 0x4904
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x7d, %ecx
movb %al, -0x481(%rbp)
je 0x486d
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2c, %eax
setne %al
movb %al, -0x481(%rbp)
movb -0x481(%rbp), %al
testb $0x1, %al
jne 0x4879
jmp 0x4889
jmp 0x487b
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x4846
movq -0x30(%rbp), %rax
movq -0x138(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x64(%rbp)
movq -0x140(%rbp), %rdi
movslq -0x60(%rbp), %rax
addq %rax, %rdi
movq -0x138(%rbp), %rsi
movslq -0x64(%rbp), %rdx
callq 0x30a0
movq -0x140(%rbp), %rdi
movslq -0x60(%rbp), %rax
addq %rax, %rdi
movslq -0x64(%rbp), %rax
addq %rax, %rdi
movq -0x48(%rbp), %rsi
addq $0x1, %rsi
callq 0x30e0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x140(%rbp), %rdx
callq 0x4660
cmpl $0x0, %eax
je 0x48ff
movl $0x1, -0x4(%rbp)
jmp 0x4dae
jmp 0x481c
cmpl $0xc8, -0x5c(%rbp)
jle 0x4929
movq -0x28(%rbp), %rdi
movq -0x140(%rbp), %rsi
leaq 0x681a(%rip), %rdx      # 0xb139
movl $0x102, %ecx            # imm = 0x102
callq 0x31f0
movl $0x0, -0x4(%rbp)
jmp 0x4dae
cmpl $0x0, -0x4c(%rbp)
je 0x4c7b
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x495b
leaq 0x687d(%rip), %rax      # 0xb1cf
movq %rax, -0x170(%rbp)
jmp 0x4966
movq -0x18(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rdi
leaq -0x3a0(%rbp), %rsi
callq 0x3380
cmpl $0x0, %eax
jne 0x4990
movl -0x388(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
je 0x499c
movl $0x0, -0x4(%rbp)
jmp 0x4dae
movq -0x170(%rbp), %rdi
callq 0x3170
movq %rax, -0x148(%rbp)
cmpq $0x0, -0x148(%rbp)
jne 0x4a1b
movq -0x10(%rbp), %rdi
callq 0x3270
movq -0x10(%rbp), %rax
movq %rax, -0x498(%rbp)
movq -0x170(%rbp), %rax
movq %rax, -0x490(%rbp)
movq -0x10(%rbp), %rdi
callq 0x31b0
movq -0x498(%rbp), %rdi
movq -0x490(%rbp), %rdx
movq %rax, %r8
leaq 0x67d5(%rip), %rsi      # 0xb1d1
leaq 0x67e8(%rip), %rcx      # 0xb1eb
xorl %eax, %eax
movl %eax, %r9d
movb $0x0, %al
callq 0x3510
movl $0x1, -0x4(%rbp)
jmp 0x4dae
movq -0x18(%rbp), %rdi
callq 0x3190
movl %eax, -0x154(%rbp)
movq -0x30(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x158(%rbp)
cmpl $0xc8, -0x158(%rbp)
jge 0x4a57
leaq -0x310(%rbp), %rax
movq %rax, -0x160(%rbp)
jmp 0x4a80
movq -0x28(%rbp), %rdi
movl -0x158(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq 0x66ca(%rip), %rdx      # 0xb139
movl $0x12e, %ecx            # imm = 0x12E
callq 0x34c0
movq %rax, -0x160(%rbp)
movq -0x160(%rbp), %rdi
movq -0x20(%rbp), %rsi
movslq -0x158(%rbp), %rdx
callq 0x30a0
movq -0x160(%rbp), %rax
movslq -0x158(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl $0x0, -0x50(%rbp)
movq -0x148(%rbp), %rdi
callq 0x33f0
movq %rax, -0x150(%rbp)
cmpq $0x0, -0x150(%rbp)
jne 0x4ad2
jmp 0x4c35
movq -0x150(%rbp), %rax
movsbl 0x13(%rax), %eax
cmpl $0x2e, %eax
jne 0x4af3
movq -0x160(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2e, %eax
je 0x4af3
jmp 0x4ab0
movq -0x150(%rbp), %rdi
addq $0x13, %rdi
movq -0x160(%rbp), %rsi
callq 0x3440
cmpl $0x0, %eax
je 0x4c30
movq -0x150(%rbp), %rdi
addq $0x13, %rdi
callq 0x3190
movl %eax, -0x3a4(%rbp)
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x4b5d
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x150(%rbp), %rcx
addq $0x13, %rcx
movl -0x3a4(%rbp), %r8d
callq 0x4dc0
jmp 0x4c2e
movl -0x154(%rbp), %eax
addl -0x3a4(%rbp), %eax
addl $0x2, %eax
cmpl $0xc8, %eax
jg 0x4b83
leaq -0x240(%rbp), %rax
movq %rax, -0x168(%rbp)
jmp 0x4bb2
movq -0x28(%rbp), %rdi
movl -0x154(%rbp), %eax
addl -0x3a4(%rbp), %eax
addl $0x2, %eax
movl %eax, %eax
movl %eax, %esi
leaq 0x6598(%rip), %rdx      # 0xb139
movl $0x14b, %ecx            # imm = 0x14B
callq 0x34c0
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x150(%rbp), %r8
addq $0x13, %r8
leaq 0x661c(%rip), %rsi      # 0xb1ef
movb $0x0, %al
callq 0x35a0
movq -0x10(%rbp), %rdi
movq -0x168(%rbp), %rsi
movq -0x30(%rbp), %rdx
addq $0x1, %rdx
callq 0x4660
movl %eax, -0x50(%rbp)
movq -0x168(%rbp), %rax
leaq -0x240(%rbp), %rcx
cmpq %rcx, %rax
je 0x4c24
movq -0x28(%rbp), %rdi
movq -0x168(%rbp), %rsi
leaq 0x651f(%rip), %rdx      # 0xb139
movl $0x150, %ecx            # imm = 0x150
callq 0x31f0
cmpl $0x0, -0x50(%rbp)
je 0x4c2c
jmp 0x4c35
jmp 0x4c2e
jmp 0x4c30
jmp 0x4ab0
movq -0x148(%rbp), %rdi
callq 0x32c0
movq -0x160(%rbp), %rax
leaq -0x310(%rbp), %rcx
cmpq %rcx, %rax
je 0x4c70
movq -0x28(%rbp), %rdi
movq -0x160(%rbp), %rsi
leaq 0x64d3(%rip), %rdx      # 0xb139
movl $0x15a, %ecx            # imm = 0x15A
callq 0x31f0
movl -0x50(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x4dae
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x4caf
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x30(%rbp), %rax
movq -0x20(%rbp), %r8
subq %r8, %rax
movl %eax, %r8d
callq 0x4dc0
jmp 0x4da7
movq -0x18(%rbp), %rdi
callq 0x3190
movl %eax, -0x3a8(%rbp)
movslq -0x3a8(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rdx
subq %rdx, %rcx
addq %rcx, %rax
addq $0x2, %rax
movl %eax, -0x3ac(%rbp)
cmpl $0xc8, -0x3ac(%rbp)
jg 0x4cf9
leaq -0x480(%rbp), %rax
movq %rax, -0x3b8(%rbp)
jmp 0x4d1d
movq -0x28(%rbp), %rdi
movl -0x3ac(%rbp), %eax
movl %eax, %esi
leaq 0x642d(%rip), %rdx      # 0xb139
movl $0x171, %ecx            # imm = 0x171
callq 0x34c0
movq %rax, -0x3b8(%rbp)
movq -0x3b8(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x30(%rbp), %rax
movq -0x20(%rbp), %rsi
subq %rsi, %rax
movl %eax, %r8d
movq -0x20(%rbp), %r9
leaq 0x64b1(%rip), %rsi      # 0xb1f6
movb $0x0, %al
callq 0x35a0
movq -0x10(%rbp), %rdi
movq -0x3b8(%rbp), %rsi
movq -0x30(%rbp), %rdx
addq $0x1, %rdx
callq 0x4660
movl %eax, -0x50(%rbp)
movq -0x3b8(%rbp), %rax
leaq -0x480(%rbp), %rcx
cmpq %rcx, %rax
je 0x4d96
movq -0x28(%rbp), %rdi
movq -0x3b8(%rbp), %rsi
leaq 0x63ad(%rip), %rdx      # 0xb139
movl $0x176, %ecx            # imm = 0x176
callq 0x31f0
cmpl $0x0, -0x50(%rbp)
je 0x4da5
movl $0x1, -0x4(%rbp)
jmp 0x4dae
jmp 0x4da7
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x4a0, %rsp            # imm = 0x4A0
popq %rbp
retq
nopw (%rax,%rax)