// File: /workspace/github_c_makefile_star2_O1/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 $0x258, %rsp            # imm = 0x258
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, 0x8(%rsp)
callq 0x3540
movq %rax, (%rsp)
movzbl (%rbx), %eax
leaq 0x5694(%rip), %rdx      # 0x9871
testl %eax, %eax
je 0x41f3
cmpl $0x2f, %eax
jne 0x41ec
cmpb $0x0, 0x1(%rbx)
je 0x41f3
leaq 0x4f96(%rip), %rdx      # 0x9189
movq %rbx, 0x10(%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 0x426a
cmpl $0x2f, %esi
je 0x426a
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 0x4214
testq %r15, %r15
je 0x429c
testq %rbx, %rbx
je 0x4327
movq %r12, %rdi
callq 0x3190
incl %eax
cmpl $0xc9, %eax
movq %rax, 0x18(%rsp)
jge 0x4343
leaq 0x30(%rsp), %rbp
jmp 0x435d
testl %ecx, %ecx
je 0x4417
movq %rdx, 0x18(%rsp)
testb %al, %al
leaq 0x4f1d(%rip), %rbx      # 0x91cf
movq 0x10(%rsp), %r15
cmovneq %r15, %rbx
leaq 0x100(%rsp), %rsi
movq %rbx, %rdi
callq 0x3380
xorl %r14d, %r14d
testl %eax, %eax
jne 0x43db
movl $0xf000, %eax           # imm = 0xF000
andl 0x118(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x43db
movq %rbx, %rdi
callq 0x3170
testq %rax, %rax
je 0x44db
movq %rax, %r14
movq %r15, %rdi
callq 0x3190
movq %rax, 0x28(%rsp)
cmpl $0xc8, %ebp
jge 0x4516
leaq 0x190(%rsp), %r15
jmp 0x4531
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x3270
leaq 0x4e72(%rip), %rax      # 0x91ad
movq %rax, (%rbx)
jmp 0x43d5
movl %eax, %esi
leaq 0x4ded(%rip), %rdx      # 0x9139
movq (%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 0x43f0
movq %r15, %rsi
incq %rsi
xorl %r12d, %r12d
movq %rsi, %r15
movzbl (%r15), %eax
cmpl $0x2c, %eax
je 0x43a2
cmpl $0x7d, %eax
je 0x43a2
incq %r15
addq %r14, %r12
jmp 0x438c
sarq $0x20, %r12
movq %r13, %rdi
movq %r12, %rdx
callq 0x30a0
addq %r13, %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x30e0
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbp, %rdx
callq 0x41ae
testl %eax, %eax
je 0x437a
movl $0x1, %r14d
movl %r14d, %eax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
cmpl $0xc9, 0x18(%rsp)
jl 0x43db
leaq 0x4d35(%rip), %rdx      # 0x9139
movq (%rsp), %rdi
movq %rbp, %rsi
movl $0x102, %ecx            # imm = 0x102
callq 0x31f0
jmp 0x43db
testb %sil, %sil
movq 0x10(%rsp), %r15
je 0x44c0
movq %rdx, %r13
movq %r15, %rdi
callq 0x3190
addq %rbp, %rax
addq $0x2, %rax
leaq 0x30(%rsp), %r14
movq %r14, %rbx
cmpl $0xc9, %eax
jl 0x4460
movl %eax, %esi
leaq 0x4cea(%rip), %rdx      # 0x9139
movq (%rsp), %rdi
movl $0x171, %ecx            # imm = 0x171
callq 0x34c0
movq %rax, %rbx
leaq 0x4d8f(%rip), %rsi      # 0x91f6
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 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x41ae
movl %eax, %ebp
cmpq %r14, %rbx
je 0x44b0
leaq 0x4c9a(%rip), %rdx      # 0x9139
movq (%rsp), %rdi
movq %rbx, %rsi
movl $0x176, %ecx            # imm = 0x176
callq 0x31f0
movl $0x1, %r14d
testl %ebp, %ebp
jne 0x43db
jmp 0x44d3
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rcx
movl %ebp, %r8d
callq 0x4683
xorl %r14d, %r14d
jmp 0x43db
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x3270
movq %r14, %rdi
callq 0x31b0
leaq 0x4cda(%rip), %rsi      # 0x91d1
leaq 0x4ced(%rip), %rcx      # 0x91eb
movq %r14, %rdi
movq %rbx, %rdx
movq %rax, %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x3510
jmp 0x43d5
leal 0x1(%rbp), %esi
leaq 0x4c19(%rip), %rdx      # 0x9139
movq (%rsp), %rdi
movl $0x12e, %ecx            # imm = 0x12E
callq 0x34c0
movq %rax, %r15
sarq $0x20, %r13
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x30a0
movq %r15, %rbx
movb $0x0, (%r15,%r13)
leaq (%r12,%rbp), %rax
incq %rax
movq %rax, 0x20(%rsp)
xorl %r15d, %r15d
movq %r14, %rdi
callq 0x33f0
testq %rax, %rax
je 0x464e
movq %rax, %r13
cmpb $0x2e, 0x13(%rax)
jne 0x4579
cmpb $0x2e, (%rbx)
jne 0x455a
addq $0x13, %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x3440
testl %eax, %eax
je 0x455a
movq %r13, %rdi
callq 0x3190
cmpb $0x0, (%r12,%rbp)
je 0x462a
addl 0x28(%rsp), %eax
leaq 0x30(%rsp), %r15
cmpl $0xc7, %eax
jl 0x45cd
addl $0x2, %eax
movq (%rsp), %rdi
movq %rax, %rsi
leaq 0x4b79(%rip), %rdx      # 0x9139
movl $0x14b, %ecx            # imm = 0x14B
callq 0x34c0
movq %rax, %r15
movq %r15, %rdi
leaq 0x4c18(%rip), %rsi      # 0x91ef
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x35a0
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq 0x20(%rsp), %rdx
callq 0x41ae
movq %r15, %rsi
movl %eax, %r15d
leaq 0x30(%rsp), %rax
cmpq %rax, %rsi
je 0x4622
movq (%rsp), %rdi
leaq 0x4b21(%rip), %rdx      # 0x9139
movl $0x150, %ecx            # imm = 0x150
callq 0x31f0
testl %r15d, %r15d
sete %al
jmp 0x4646
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r13, %rcx
movl %eax, %r8d
callq 0x4683
movb $0x1, %al
testb %al, %al
jne 0x455a
movq %r14, %rdi
callq 0x32c0
leaq 0x190(%rsp), %rax
cmpq %rax, %rbx
je 0x467b
leaq 0x4acf(%rip), %rdx      # 0x9139
movq (%rsp), %rdi
movq %rbx, %rsi
movl $0x15a, %ecx            # imm = 0x15A
callq 0x31f0
movl %r15d, %r14d
jmp 0x43db