// File: /workspace/github_c_makefile_star2_O3/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 $0x138, %rsp            # imm = 0x138
movl %edx, %r14d
movq %rsi, (%rsp)
xorl %eax, %eax
callq 0x4246
decl %r14d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
leaq 0x17a9(%rip), %r15      # 0x97c8
leaq 0x177a(%rip), %rbp      # 0x97a0
xorl %r13d, %r13d
xorl %ebx, %ebx
movq $0x0, 0x28(%rsp)
movl %r14d, 0xc(%rsp)
movl %ebx, %ecx
movl %r13d, 0x1c(%rsp)
movl %ecx, 0x10(%rsp)
movl %r12d, %ebx
xorl %eax, %eax
callq 0x7c80
leal -0x7(%rax), %ecx
cmpl $0xe, %ecx
ja 0x8066
leaq 0x1707(%rip), %rdx      # 0x9764
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpl $0x20, %eax
je 0x809a
cmpl $0x7f, %eax
je 0x818e
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %ebx, %ecx
cmpl %r14d, %r13d
jge 0x8040
cmpl $0x1f, %eax
jg 0x8453
xorl %eax, %eax
leaq 0x57d5(%rip), %rcx      # 0xd868
callq *0x50(%rcx)
movl %ebx, %ecx
jmp 0x8040
testl %r13d, %r13d
je 0x831f
movslq %r13d, %r12
movl $0x0, 0x20(%rsp)
movb $0x8, %al
movl $0x1, %r14d
movsbl %al, %edi
xorl %eax, %eax
callq 0x7c59
movb (%r14,%rbp), %al
incq %r14
cmpq $0x4, %r14
jne 0x80b6
leaq 0x5090(%rip), %rax      # 0xd164
decl (%rax)
movq (%rsp), %rax
movb -0x1(%rax,%r12), %al
decq %r12
cmpb $0x1f, %al
jg 0x8163
movb $0x8, %al
movl $0x1, %r14d
movsbl %al, %edi
xorl %eax, %eax
callq 0x7c59
movb (%r14,%rbp), %al
incq %r14
cmpq $0x4, %r14
jne 0x80ee
leaq 0x5058(%rip), %rax      # 0xd164
decl (%rax)
movq (%rsp), %rax
movb (%rax,%r12), %al
cmpb $0xa, %al
jne 0x8163
movq %r15, %r13
movl %ebx, %r15d
movb $0x8, %al
movl $0x1, %r14d
leaq 0x1675(%rip), %rbx      # 0x97a4
movsbl %al, %edi
xorl %eax, %eax
callq 0x7c59
movb (%r14,%rbx), %al
incq %r14
cmpq $0x7, %r14
jne 0x812f
leaq 0x5017(%rip), %rax      # 0xd164
addl $-0x2, (%rax)
movq (%rsp), %rax
movb (%rax,%r12), %al
movl %r15d, %ebx
movq %r13, %r15
movl 0x1c(%rsp), %r13d
cmpb $0x3f, %al
je 0x816f
movzbl %al, %eax
cmpl $0x2a, %eax
jne 0x8177
movl $0x1, 0x20(%rsp)
testq %r12, %r12
jne 0x80ae
cmpl $0x0, 0x20(%rsp)
sete %r14b
jmp 0x8322
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %r13d, %eax
xorl %r13d, %r13d
testl %eax, %eax
je 0x8039
movl %ebx, 0x10(%rsp)
movb $0x8, %al
movl $0x1, %ebx
movsbl %al, %edi
xorl %eax, %eax
callq 0x7c59
movb (%rbx,%rbp), %al
incq %rbx
cmpq $0x4, %rbx
jne 0x81ad
leaq 0x4f9a(%rip), %rax      # 0xd164
decl (%rax)
movslq 0x1c(%rsp), %rax
leaq -0x1(%rax), %r13
movq (%rsp), %rcx
cmpb $0x1f, -0x1(%rcx,%rax)
jg 0x823f
movb $0x8, %al
movl $0x1, %ebx
movsbl %al, %edi
xorl %eax, %eax
callq 0x7c59
movb (%rbx,%rbp), %al
incq %rbx
cmpq $0x4, %rbx
jne 0x81e7
leaq 0x4f60(%rip), %rax      # 0xd164
decl (%rax)
movq (%rsp), %rax
cmpb $0xa, (%rax,%r13)
jne 0x823f
movb $0x8, %al
movl $0x1, %ebx
movsbl %al, %edi
xorl %eax, %eax
callq 0x7c59
leaq 0x157b(%rip), %rax      # 0x97a4
movb (%rbx,%rax), %al
incq %rbx
cmpq $0x7, %rbx
jne 0x8218
leaq 0x4f28(%rip), %rax      # 0xd164
addl $-0x2, (%rax)
xorl %eax, %eax
callq 0x7c68
movl 0x10(%rsp), %ebx
jmp 0x8039
movl %ebx, 0x10(%rsp)
testl %r13d, %r13d
leaq 0x4f07(%rip), %r12      # 0xd164
je 0x8301
movslq %r13d, %rbx
movq %rbx, %r14
movb $0x8, %al
movl $0x1, %ebx
movsbl %al, %edi
xorl %eax, %eax
callq 0x7c59
movb (%rbx,%rbp), %al
incq %rbx
cmpq $0x4, %rbx
jne 0x8270
decl (%r12)
leaq -0x1(%r14), %rbx
movq (%rsp), %rax
cmpb $0x1f, -0x1(%rax,%r14)
jg 0x82f8
movb $0x8, %al
movl $0x1, %r14d
movsbl %al, %edi
xorl %eax, %eax
callq 0x7c59
movb (%r14,%rbp), %al
incq %r14
cmpq $0x4, %r14
jne 0x82a2
decl (%r12)
movq (%rsp), %rax
cmpb $0xa, (%rax,%rbx)
jne 0x82f8
movq %r12, %r13
movb $0x8, %al
movl $0x1, %r14d
leaq 0x14cb(%rip), %r12      # 0x97a4
movsbl %al, %edi
xorl %eax, %eax
callq 0x7c59
movb (%r14,%r12), %al
incq %r14
cmpq $0x7, %r14
jne 0x82d9
movq %r13, %r12
addl $-0x2, (%r13)
testq %rbx, %rbx
jne 0x8266
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x7c68
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl 0xc(%rsp), %r14d
movl 0x10(%rsp), %ebx
jmp 0x8039
movb $0x1, %r14b
xorl %eax, %eax
callq 0x7c68
testl %ebx, %ebx
movq 0x28(%rsp), %r12
js 0x8374
movl 0x10(%rsp), %ebx
testl %ebx, %ebx
jle 0x8484
movl %ebx, %r14d
movq %r12, %rdi
callq 0x2280
cmpl $0x20, %eax
je 0x8353
cmpl $-0x1, %eax
jne 0x8341
leal -0x1(%r14), %ecx
cmpl $0x1, %r14d
movl %ecx, %r14d
jg 0x8341
cmpl $0x20, %eax
jne 0x848d
movl 0x10(%rsp), %ebx
incl %ebx
jmp 0x849b
movslq %r13d, %rax
movq (%rsp), %rcx
movb $0x0, (%rcx,%rax)
testq %r12, %r12
je 0x838c
movq %r12, %rdi
callq 0x20b0
movabsq $0x58585858585865, %rax # imm = 0x58585858585865
movq %rax, 0x5531(%rip)      # 0xd8ce
movabsq $0x58656d2f706d742f, %rax # imm = 0x58656D2F706D742F
movq %rax, 0x551a(%rip)      # 0xd8c8
movl $0x6f686365, 0x30(%rsp) # imm = 0x6F686365
movw $0x20, 0x34(%rsp)
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x2240
testb %r14b, %r14b
je 0x83e2
movq %rbx, %rdi
callq 0x20d0
movw $0x2a, 0x30(%rsp,%rax)
movq %rbx, %rdi
callq 0x20d0
movw $0x3e20, 0x30(%rsp,%rax) # imm = 0x3E20
movb $0x0, 0x32(%rsp,%rax)
leaq 0x54cb(%rip), %r12      # 0xd8c8
movq %r12, %rdi
callq 0x21c0
cmpl $-0x1, %eax
movl 0xc(%rsp), %r14d
je 0x861b
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2240
movq %rbx, %rdi
callq 0x20d0
movl $0x263e3220, 0x30(%rsp,%rax) # imm = 0x263E3220
movw $0x31, 0x34(%rsp,%rax)
movq %rbx, %rdi
callq 0x20f0
movq %r12, %rdi
leaq 0xd82(%rip), %rsi       # 0x91c9
callq 0x2230
movq %rax, %r12
xorl %ebx, %ebx
jmp 0x8489
movslq %r13d, %rcx
incl %r13d
movq (%rsp), %rdx
movb %al, (%rdx,%rcx)
movl %eax, %edi
xorl %eax, %eax
callq 0x7c59
leaq 0x4cf4(%rip), %rax      # 0xd164
incl (%rax)
xorl %eax, %eax
callq 0x7c68
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x8039
movl 0xc(%rsp), %r14d
incl %ebx
jmp 0x84a0
xorl %ebx, %ebx
xorl %eax, %eax
leaq 0x53d0(%rip), %rcx      # 0xd868
callq *0x50(%rcx)
movl 0xc(%rsp), %r14d
xorl %r13d, %r13d
movq %r12, %rdi
callq 0x2280
cmpl $0x1f, %eax
jg 0x84bc
cmpl $-0x1, %eax
je 0x84e6
cmpl $0xa, %eax
jne 0x84c6
jmp 0x84e6
cmpl $0x20, %eax
je 0x84e6
cmpl $0x2a, %eax
je 0x84da
cmpl %r14d, %r13d
jge 0x84a3
movl %r13d, %ecx
incl %r13d
movq (%rsp), %rdx
movb %al, (%rdx,%rcx)
jmp 0x84a3
xorl %eax, %eax
leaq 0x5385(%rip), %rcx      # 0xd868
callq *0x50(%rcx)
movq %r12, 0x28(%rsp)
testl %r13d, %r13d
je 0x859e
movl %r13d, %eax
movq %rax, 0x10(%rsp)
xorl %r14d, %r14d
movq %r13, 0x20(%rsp)
movq (%rsp), %rax
movsbl (%rax,%r14), %r12d
cmpl $0xa, %r12d
sete %al
cmpl $0x20, %r12d
setge %cl
orb %al, %cl
jne 0x8538
movl $0x5e, %edi
xorl %eax, %eax
callq 0x7c59
leaq 0x4c32(%rip), %rax      # 0xd164
incl (%rax)
xorl $0x40, %r12d
cmpl $0xa, %r12d
jne 0x8579
movl %ebx, %r13d
movb $0x3c, %al
xorl %r12d, %r12d
leaq 0x1(%r12), %rbx
movsbl %al, %edi
xorl %eax, %eax
callq 0x7c59
movb 0x1(%r12,%r15), %al
movq %rbx, %r12
cmpq $0x4, %rbx
jne 0x8546
leaq 0x4bfa(%rip), %rcx      # 0xd164
movl (%rcx), %eax
addl $0x3, %eax
movl %r13d, %ebx
movq 0x20(%rsp), %r13
jmp 0x858c
movl %r12d, %edi
xorl %eax, %eax
callq 0x7c59
leaq 0x4bda(%rip), %rcx      # 0xd164
movl (%rcx), %eax
incl %eax
movl %eax, (%rcx)
incq %r14
cmpq 0x10(%rsp), %r14
jne 0x8504
xorl %eax, %eax
callq 0x7c68
movq 0x28(%rsp), %rdi
callq 0x2100
leaq 0x5312(%rip), %rdi      # 0xd8c8
callq 0x2060
movl %ebx, %r12d
movl 0xc(%rsp), %r14d
jmp 0x8039
movslq %r13d, %rax
movq (%rsp), %rbx
movb $0x0, (%rbx,%rax)
leaq 0x11d0(%rip), %rdi      # 0x97aa
xorl %eax, %eax
callq 0x4246
xorl %eax, %eax
callq 0x7c68
xorl %eax, %eax
cmpb $0x0, (%rbx)
setne %al
jmp 0x8609
xorl %edi, %edi
xorl %esi, %esi
xorl %eax, %eax
callq 0x5d72
xorl %eax, %eax
callq 0x7c68
movl $0x2, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11ab(%rip), %rdi      # 0x97cd
callq 0x2080
movl $0x1, %edi
callq 0x2260