// File: /workspace/github_c_makefile_star2_O2/hughbarney[P]pEmacs/complete.c
// Function: getfilename
int getfilename(char *prompt, char *buf, int nbuf)
{
	int cpos = 0;	/* current character position in string */
	int c;
	int ocpos, nskip = 0, didtry = 0;
	int eolchar = '\n';
	int result = 0;

	static char tmp[] = "/tmp/meXXXXXX";
	FILE *tmpf = NULL;

	/* prompt the user for the input string */
	mlwrite(prompt);

	for (;;) {
		if (!didtry)
			nskip = -1;
		didtry = 0;

		/* get a character from the user */
		c = ttgetc();

		/* If it is a <ret>, change it to a <NL> */
		if (c == CTRL_M)
			c = '\n';

		/* if they hit the line terminate, wrap it up */
		if (c == eolchar) {
			buf[cpos++] = 0;

			/* clear the message line */
			mlwrite("");
			ttflush();

			/* if we default the buffer, return FALSE */
			if (buf[0] == 0)
				return FALSE;

			return TRUE;
		}

		if (c == CTRL_G) {
			/* Abort the input? */
			ctrlg(FALSE, 0);
			ttflush();
			return ABORT;
		} else if ((c == 0x7F || c == 0x08)) {
			/* rubout/erase */
			if (cpos != 0) {
				outstring("\b \b");
				--ttcol;
				if (buf[--cpos] < 0x20) {
					outstring("\b \b");
					--ttcol;
				}
				if (buf[cpos] == '\n') {
					outstring("\b\b  \b\b");
					ttcol -= 2;
				}
				ttflush();
			}
		} else if (c == 0x15) {
			/* C-U, kill */
			while (cpos != 0) {
				outstring("\b \b");
				--ttcol;

				if (buf[--cpos] < 0x20) {
					outstring("\b \b");
					--ttcol;
				}
				if (buf[cpos] == '\n') {
					outstring("\b\b  \b\b");
					ttcol -= 2;
				}
			}
			ttflush();
		} else if ((c == 0x09 || c == ' ')) {
			/* TAB, complete file name */
			char ffbuf[255];
			int n, iswild = 0;

			didtry = 1;
			ocpos = cpos;
			while (cpos != 0) {
				outstring("\b \b");
				--ttcol;

				if (buf[--cpos] < 0x20) {
					outstring("\b \b");
					--ttcol;
				}
				if (buf[cpos] == '\n') {
					outstring("\b\b  \b\b");
					ttcol -= 2;
				}
				if (buf[cpos] == '*' || buf[cpos] == '?')
					iswild = 1;
			}
			ttflush();
			if (nskip < 0) {
				buf[ocpos] = 0;
				if (tmpf != NULL)
					fclose(tmpf);
				strcpy(tmp, "/tmp/meXXXXXX");
				strcpy(ffbuf, "echo ");
				strcat(ffbuf, buf);
				if (!iswild)
					strcat(ffbuf, "*");
				strcat(ffbuf, " >");
				result = mkstemp(tmp);
				if (result == -1)
				{
				  printf("Failed to create temp file\n");
				  exit(1);
				}
				strcat(ffbuf, tmp);
				strcat(ffbuf, " 2>&1");
				result = system(ffbuf);
				tmpf = fopen(tmp, "r");
				nskip = 0;
			}
			c = ' ';
			for (n = nskip; n > 0; n--)
				while ((c = getc(tmpf)) != EOF
				       && c != ' ');
			nskip++;

			if (c != ' ') {
				(*term.t_beep) ();
				nskip = 0;
			}
			while ((c = getc(tmpf)) != EOF && c != '\n'
			       && c != ' ' && c != '*')
			{
				if (cpos < nbuf - 1)
					buf[cpos++] = c;
			}
			if (c == '*')
				(*term.t_beep) ();

			for (n = 0; n < cpos; n++) {
				c = buf[n];
				if ((c < ' ') && (c != '\n')) {
					outstring("^");
					++ttcol;
					c ^= 0x40;
				}

				if (c != '\n') {
					ttputc(c);
				} else {	/* put out <NL> for <ret> */
					outstring("<NL>");
					ttcol += 3;
				}
				++ttcol;
			}
			ttflush();
			rewind(tmpf);
			unlink(tmp);
		} else {
			if (cpos < nbuf - 1) {
				/* if a control char */
				if ((c < ' ') && (c != '\n')) {
				  (*term.t_beep) ();
				} else {
				  buf[cpos++] = c;
				  ttputc(c);
				  ++ttcol;
				  ttflush();
				}
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movl %edx, %ebx
movq %rsi, %r12
xorl %eax, %eax
callq 0x3dec
decl %ebx
pushq $-0x1
popq %rcx
movq $0x0, 0x8(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
movl %ebx, 0x10(%rsp)
movq 0x8(%rsp), %rax
movl %eax, %r13d
movl %ebp, 0x8(%rsp)
movl %ecx, %ebp
xorl %eax, %eax
callq 0x71cd
leal -0x7(%rax), %ecx
cmpl $0xe, %ecx
ja 0x7581
leaq 0x1204(%rip), %rdx      # 0x877c
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpl $0x20, %eax
je 0x75b9
cmpl $0x7f, %eax
je 0x763f
movl %ebp, 0x8(%rsp)
pushq $-0x1
popq %rcx
cmpl %ebx, %r13d
jge 0x7560
cmpl $0x1f, %eax
jg 0x7895
xorl %eax, %eax
leaq 0x52ab(%rip), %rcx      # 0xc858
callq *0x50(%rcx)
movl %ebp, 0x8(%rsp)
pushq $-0x1
popq %rcx
jmp 0x7560
movslq %r13d, %rbx
movb $0x1, %al
movl %eax, 0x14(%rsp)
movq %rbx, 0x18(%rsp)
movq %rbx, %r13
subq $0x1, %rbx
jb 0x76b8
movl %ebp, %r14d
leaq 0x11da(%rip), %rdi      # 0x87b8
callq 0x79d2
leaq 0x4b6a(%rip), %rbp      # 0xc154
decl (%rbp)
movb -0x1(%r12,%r13), %al
cmpb $0x1f, %al
jg 0x7623
leaq 0x11bb(%rip), %rdi      # 0x87b8
callq 0x79d2
decl (%rbp)
movb -0x1(%r12,%r13), %al
cmpb $0xa, %al
jne 0x7623
leaq 0x11a7(%rip), %rdi      # 0x87bc
callq 0x79d2
addl $-0x2, (%rbp)
movb -0x1(%r12,%r13), %al
cmpb $0x3f, %al
movl %r14d, %ebp
je 0x7635
movzbl %al, %eax
movq %rbx, %r13
cmpl $0x2a, %eax
jne 0x75ca
movl $0x0, 0x14(%rsp)
jmp 0x75c7
movq $0x0, 0x8(%rsp)
pushq $-0x1
popq %rcx
testl %r13d, %r13d
je 0x7554
leaq 0x115d(%rip), %rdi      # 0x87b8
callq 0x79d2
leaq 0x4aed(%rip), %r14      # 0xc154
decl (%r14)
movslq %r13d, %rax
leaq -0x1(%rax), %rcx
movq %rcx, 0x8(%rsp)
cmpb $0x1f, -0x1(%r12,%rax)
jg 0x76a9
leaq 0x1133(%rip), %rdi      # 0x87b8
callq 0x79d2
decl (%r14)
movq 0x8(%rsp), %rax
cmpb $0xa, (%r12,%rax)
jne 0x76a9
leaq 0x111c(%rip), %rdi      # 0x87bc
callq 0x79d2
addl $-0x2, (%r14)
xorl %eax, %eax
callq 0x71b5
pushq $-0x1
popq %rcx
jmp 0x7554
xorl %eax, %eax
callq 0x71b5
testl %ebp, %ebp
js 0x773e
movl 0x8(%rsp), %ebp
jmp 0x7816
movslq %r13d, %rbx
leaq 0x4a7e(%rip), %r13      # 0xc154
movq %rbx, %r14
subq $0x1, %rbx
jb 0x7722
leaq 0x10d2(%rip), %rdi      # 0x87b8
callq 0x79d2
decl (%r13)
cmpb $0x20, -0x1(%r12,%r14)
jge 0x76d6
leaq 0x10ba(%rip), %rdi      # 0x87b8
callq 0x79d2
decl (%r13)
cmpb $0xa, -0x1(%r12,%r14)
jne 0x76d6
leaq 0x10a6(%rip), %rdi      # 0x87bc
callq 0x79d2
addl $-0x2, (%r13)
jmp 0x76d6
movq $0x0, 0x8(%rsp)
xorl %eax, %eax
callq 0x71b5
pushq $-0x1
popq %rcx
movl 0x10(%rsp), %ebx
jmp 0x7554
movq 0x18(%rsp), %rax
movb $0x0, (%r12,%rax)
testq %r15, %r15
je 0x7755
movq %r15, %rdi
callq 0x20b0
movabsq $0x58585858585865, %rax # imm = 0x58585858585865
movq %rax, 0x5158(%rip)      # 0xc8be
movabsq $0x58656d2f706d742f, %rax # imm = 0x58656D2F706D742F
movq %rax, 0x5141(%rip)      # 0xc8b8
movl $0x6f686365, 0x20(%rsp) # imm = 0x6F686365
movw $0x20, 0x24(%rsp)
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2230
testb $0x1, 0x14(%rsp)
leaq 0x5116(%rip), %r14      # 0xc8b8
je 0x77b3
movq %rbx, %rdi
callq 0x20d0
movw $0x2a, 0x20(%rsp,%rax)
movq %rbx, %rdi
callq 0x20d0
movw $0x3e20, 0x20(%rsp,%rax) # imm = 0x3E20
movb $0x0, 0x22(%rsp,%rax)
movq %r14, %rdi
callq 0x21b0
cmpl $-0x1, %eax
je 0x79be
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2230
movq %rbx, %rdi
callq 0x20d0
movl $0x263e3220, 0x20(%rsp,%rax) # imm = 0x263E3220
movw $0x31, 0x24(%rsp,%rax)
movq %rbx, %rdi
callq 0x20f0
movq %r14, %rdi
leaq 0x9bd(%rip), %rsi       # 0x81c9
callq 0x2220
movq %rax, %r15
xorl %ebp, %ebp
pushq $0x20
popq %rax
movl %ebp, %ebx
testl %ebx, %ebx
jle 0x7835
movq %r15, %rdi
callq 0x2270
cmpl $0x20, %eax
je 0x7831
cmpl $-0x1, %eax
jne 0x781f
decl %ebx
jmp 0x781b
cmpl $0x20, %eax
jne 0x783e
incl %ebp
jmp 0x784c
xorl %ebp, %ebp
xorl %eax, %eax
leaq 0x500f(%rip), %rcx      # 0xc858
callq *0x50(%rcx)
movl 0x10(%rsp), %ebx
xorl %r14d, %r14d
movq $0x0, 0x8(%rsp)
movq %r15, %rdi
callq 0x2270
cmpl $-0x1, %eax
je 0x78d4
cmpl $0xa, %eax
je 0x78d4
cmpl $0x20, %eax
je 0x78d4
cmpl $0x2a, %eax
je 0x78c8
cmpl %ebx, 0x8(%rsp)
jge 0x785c
movq 0x8(%rsp), %rdx
movl %edx, %ecx
incl %edx
movq %rdx, 0x8(%rsp)
movb %al, (%r12,%rcx)
incq %r14
jmp 0x785c
movslq %r13d, %rcx
incl %r13d
movb %al, (%r12,%rcx)
movl %eax, %edi
xorl %eax, %eax
callq 0x71a6
leaq 0x48a5(%rip), %rax      # 0xc154
incl (%rax)
xorl %eax, %eax
callq 0x71b5
movl %r13d, %eax
movq %rax, 0x8(%rsp)
pushq $-0x1
popq %rcx
jmp 0x7554
xorl %eax, %eax
leaq 0x4f87(%rip), %rcx      # 0xc858
callq *0x50(%rcx)
movq %r12, %rbx
subq $0x1, %r14
jb 0x7948
movsbl (%rbx), %r13d
cmpl $0xa, %r13d
sete %al
cmpl $0x20, %r13d
setge %cl
orb %al, %cl
jne 0x790c
leaq 0xee6(%rip), %rdi       # 0x87e0
callq 0x79d2
leaq 0x484e(%rip), %rax      # 0xc154
incl (%rax)
xorl $0x40, %r13d
cmpl $0xa, %r13d
jne 0x792c
leaq 0xec9(%rip), %rdi       # 0x87e2
callq 0x79d2
leaq 0x482f(%rip), %rcx      # 0xc154
movl (%rcx), %eax
addl $0x3, %eax
jmp 0x793f
movl %r13d, %edi
xorl %eax, %eax
callq 0x71a6
leaq 0x4817(%rip), %rcx      # 0xc154
movl (%rcx), %eax
incl %eax
movl %eax, (%rcx)
incq %rbx
jmp 0x78d7
xorl %eax, %eax
callq 0x71b5
movq %r15, %rdi
callq 0x2100
leaq 0x4f5a(%rip), %rdi      # 0xc8b8
callq 0x2060
movl %ebp, %ecx
movl 0x10(%rsp), %ebx
jmp 0x7554
movslq %r13d, %rax
movb $0x0, (%r12,%rax)
leaq 0xe45(%rip), %rdi       # 0x87c2
xorl %eax, %eax
callq 0x3dec
xorl %eax, %eax
callq 0x71b5
xorl %eax, %eax
cmpb $0x0, (%r12)
setne %al
jmp 0x79ac
xorl %edi, %edi
xorl %esi, %esi
xorl %eax, %eax
callq 0x5694
xorl %eax, %eax
callq 0x71b5
pushq $0x2
popq %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe22(%rip), %rdi       # 0x87e7
callq 0x2080
pushq $0x1
popq %rdi
callq 0x2250