// File: /workspace/github_c_makefile_star2_O1/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, %ebx
movq %rsi, %r15
xorl %eax, %eax
callq 0x43cc
decl %ebx
movl %ebx, 0x1c(%rsp)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
leaq 0x1810(%rip), %rbp      # 0x97b8
leaq 0x17cd(%rip), %rbx      # 0x977c
movq $0x0, 0x10(%rsp)
xorl %r13d, %r13d
movq %r15, 0x28(%rsp)
movq 0x10(%rsp), %rax
movl %eax, %r14d
xorl %eax, %eax
callq 0x7c0c
leal -0x7(%rax), %ecx
cmpl $0xe, %ecx
ja 0x7fe0
movslq (%rbx,%rcx,4), %rcx
addq %rbx, %rcx
jmpq *%rcx
cmpl $0x20, %eax
je 0x8012
cmpl $0x7f, %eax
je 0x80ef
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl 0x1c(%rsp), %r14d
jge 0x7fc8
cmpl $0x1f, %eax
jg 0x83f6
xorl %eax, %eax
leaq 0x585b(%rip), %rcx      # 0xd868
callq *0x50(%rcx)
jmp 0x7fc8
testl %r14d, %r14d
je 0x8288
movl %r12d, 0xc(%rsp)
movslq %r14d, %r12
movl $0x0, 0x20(%rsp)
movb $0x8, %al
movl $0x1, %ebx
movsbl %al, %edi
xorl %eax, %eax
callq 0x7be5
movb (%rbx,%rbp), %al
incq %rbx
cmpq $0x4, %rbx
jne 0x8032
leaq 0x5115(%rip), %rax      # 0xd164
decl (%rax)
cmpb $0x1f, -0x1(%r15,%r12)
jg 0x807f
movb $0x8, %al
movl $0x1, %ebx
movsbl %al, %edi
xorl %eax, %eax
callq 0x7be5
movb (%rbx,%rbp), %al
incq %rbx
cmpq $0x4, %rbx
jne 0x8060
leaq 0x50e7(%rip), %rax      # 0xd164
decl (%rax)
decq %r12
cmpb $0xa, (%r15,%r12)
jne 0x80bd
movb $0x8, %al
movl $0x1, %ebx
leaq 0x1725(%rip), %r15      # 0x97bc
movsbl %al, %edi
xorl %eax, %eax
callq 0x7be5
movb (%rbx,%r15), %al
incq %rbx
cmpq $0x7, %rbx
jne 0x8097
leaq 0x50af(%rip), %rax      # 0xd164
addl $-0x2, (%rax)
movq 0x28(%rsp), %r15
movzbl (%r15,%r12), %eax
cmpl $0x3f, %eax
je 0x80cc
cmpl $0x2a, %eax
jne 0x80d4
movl $0x1, 0x20(%rsp)
testq %r12, %r12
jne 0x802b
cmpl $0x0, 0x20(%rsp)
sete %bl
movl 0xc(%rsp), %r12d
jmp 0x828a
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq $0x0, 0x10(%rsp)
testl %r14d, %r14d
je 0x7fc0
movb $0x8, %al
movl $0x1, %ebx
movsbl %al, %edi
xorl %eax, %eax
callq 0x7be5
movb (%rbx,%rbp), %al
incq %rbx
cmpq $0x4, %rbx
jne 0x810e
leaq 0x5039(%rip), %rax      # 0xd164
decl (%rax)
movslq %r14d, %rax
cmpb $0x1f, -0x1(%r15,%rax)
jg 0x8168
movq %rax, 0x10(%rsp)
movb $0x8, %al
movl $0x1, %ebx
movsbl %al, %edi
xorl %eax, %eax
callq 0x7be5
movb (%rbx,%rbp), %al
incq %rbx
cmpq $0x4, %rbx
jne 0x8144
leaq 0x5003(%rip), %rax      # 0xd164
decl (%rax)
movq 0x10(%rsp), %rax
decq %rax
movq %rax, 0x10(%rsp)
cmpb $0xa, (%r15,%rax)
jne 0x81ac
movb $0x8, %al
movl $0x1, %ebx
leaq 0x1637(%rip), %r12      # 0x97bc
movsbl %al, %edi
xorl %eax, %eax
callq 0x7be5
movb (%rbx,%r12), %al
incq %rbx
cmpq $0x7, %rbx
jne 0x8185
leaq 0x4fc1(%rip), %rax      # 0xd164
addl $-0x2, (%rax)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
xorl %eax, %eax
callq 0x7bf4
leaq 0x15c2(%rip), %rbx      # 0x977c
jmp 0x7fc0
testl %r14d, %r14d
je 0x8266
movslq %r14d, %r12
movb $0x8, %al
movl $0x1, %ebx
movsbl %al, %edi
xorl %eax, %eax
callq 0x7be5
movb (%rbx,%rbp), %al
incq %rbx
cmpq $0x4, %rbx
jne 0x81d2
leaq 0x4f75(%rip), %rax      # 0xd164
decl (%rax)
cmpb $0x1f, -0x1(%r15,%r12)
jg 0x821f
movb $0x8, %al
movl $0x1, %ebx
movsbl %al, %edi
xorl %eax, %eax
callq 0x7be5
movb (%rbx,%rbp), %al
incq %rbx
cmpq $0x4, %rbx
jne 0x8200
leaq 0x4f47(%rip), %rax      # 0xd164
decl (%rax)
decq %r12
cmpb $0xa, (%r15,%r12)
jne 0x825d
movb $0x8, %al
movl $0x1, %ebx
leaq 0x1585(%rip), %r15      # 0x97bc
movsbl %al, %edi
xorl %eax, %eax
callq 0x7be5
movb (%rbx,%r15), %al
incq %rbx
cmpq $0x7, %rbx
jne 0x8237
leaq 0x4f0f(%rip), %rax      # 0xd164
addl $-0x2, (%rax)
movq 0x28(%rsp), %r15
testq %r12, %r12
jne 0x81cb
movq $0x0, 0x10(%rsp)
xorl %eax, %eax
callq 0x7bf4
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
leaq 0x14f9(%rip), %rbx      # 0x977c
jmp 0x7fc0
movb $0x1, %bl
xorl %eax, %eax
callq 0x7bf4
testl %r12d, %r12d
js 0x829e
movl %r12d, %eax
jmp 0x8371
movslq %r14d, %rax
movb $0x0, (%r15,%rax)
testq %r13, %r13
je 0x82b3
movq %r13, %rdi
callq 0x20b0
movabsq $0x58585858585865, %rax # imm = 0x58585858585865
movq %rax, 0x560a(%rip)      # 0xd8ce
movabsq $0x58656d2f706d742f, %rax # imm = 0x58656D2F706D742F
movq %rax, 0x55f3(%rip)      # 0xd8c8
movl $0x6f686365, 0x30(%rsp) # imm = 0x6F686365
movw $0x20, 0x34(%rsp)
leaq 0x30(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x2240
testb %bl, %bl
je 0x8307
movq %r12, %rdi
callq 0x20d0
movw $0x2a, 0x30(%rsp,%rax)
movq %r12, %rdi
callq 0x20d0
movw $0x3e20, 0x30(%rsp,%rax) # imm = 0x3E20
movb $0x0, 0x32(%rsp,%rax)
leaq 0x55a6(%rip), %rbx      # 0xd8c8
movq %rbx, %rdi
callq 0x21c0
cmpl $-0x1, %eax
je 0x8557
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2240
movq %r12, %rdi
callq 0x20d0
movl $0x263e3220, 0x30(%rsp,%rax) # imm = 0x263E3220
movw $0x31, 0x34(%rsp,%rax)
movq %r12, %rdi
callq 0x20f0
movq %rbx, %rdi
leaq 0xe62(%rip), %rsi       # 0x91c9
callq 0x2230
movq %rax, %r13
xorl %eax, %eax
leaq 0x54f0(%rip), %r12      # 0xd868
testl %eax, %eax
movl %eax, 0xc(%rsp)
je 0x83a6
movl %eax, %ebx
movq %r13, %rdi
callq 0x2280
cmpl $0x20, %eax
je 0x8394
cmpl $-0x1, %eax
jne 0x8382
leal -0x1(%rbx), %ecx
cmpl $0x1, %ebx
movl %ecx, %ebx
jg 0x8382
cmpl $0x20, %eax
sete %al
jmp 0x83a8
movb $0x1, %al
testb %al, %al
je 0x83b2
incl 0xc(%rsp)
jmp 0x83c1
movl $0x0, 0xc(%rsp)
xorl %eax, %eax
callq *0x50(%r12)
xorl %ebx, %ebx
movq %r13, %rdi
callq 0x2280
cmpl $0x1f, %eax
jg 0x83dc
cmpl $-0x1, %eax
je 0x8433
cmpl $0xa, %eax
jne 0x83e6
jmp 0x8433
cmpl $0x20, %eax
je 0x8433
cmpl $0x2a, %eax
je 0x842c
cmpl 0x1c(%rsp), %ebx
jge 0x83c3
movl %ebx, %ecx
incl %ebx
movb %al, (%r15,%rcx)
jmp 0x83c3
movslq %r14d, %rcx
incl %r14d
movb %al, (%r15,%rcx)
movl %eax, %edi
xorl %eax, %eax
callq 0x7be5
leaq 0x4d54(%rip), %rax      # 0xd164
incl (%rax)
xorl %eax, %eax
callq 0x7bf4
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %r14d, %eax
movq %rax, 0x10(%rsp)
jmp 0x7fc0
xorl %eax, %eax
callq *0x50(%r12)
movq %rbx, 0x10(%rsp)
testl %ebx, %ebx
leaq 0x4d23(%rip), %r14      # 0xd164
je 0x84da
movl 0x10(%rsp), %eax
movq %rax, 0x20(%rsp)
xorl %ebx, %ebx
movsbl (%r15,%rbx), %r12d
cmpl $0xa, %r12d
sete %al
cmpl $0x20, %r12d
setge %cl
orb %al, %cl
jne 0x847c
movl $0x5e, %edi
xorl %eax, %eax
callq 0x7be5
incl (%r14)
xorl $0x40, %r12d
cmpl $0xa, %r12d
jne 0x84bf
movb $0x3c, %al
xorl %r12d, %r12d
leaq 0x1352(%rip), %r14      # 0x97e0
leaq 0x1(%r12), %r15
movsbl %al, %edi
xorl %eax, %eax
callq 0x7be5
movb 0x1(%r12,%r14), %al
movq %r15, %r12
cmpq $0x4, %r15
jne 0x848e
leaq 0x4cb2(%rip), %rax      # 0xd164
addl $0x3, (%rax)
movq 0x28(%rsp), %r15
movq %rax, %r14
jmp 0x84c9
movl %r12d, %edi
xorl %eax, %eax
callq 0x7be5
incl (%r14)
incq %rbx
cmpq 0x20(%rsp), %rbx
jne 0x8452
xorl %eax, %eax
callq 0x7bf4
movq %r13, %rdi
callq 0x2100
leaq 0x53d8(%rip), %rdi      # 0xd8c8
callq 0x2060
leaq 0x1280(%rip), %rbx      # 0x977c
movl 0xc(%rsp), %r12d
jmp 0x7fc0
movslq %r14d, %rax
movb $0x0, (%r15,%rax)
leaq 0x12ad(%rip), %rdi      # 0x97c2
xorl %eax, %eax
callq 0x43cc
xorl %eax, %eax
callq 0x7bf4
xorl %eax, %eax
cmpb $0x0, (%r15)
setne %al
jmp 0x8545
xorl %edi, %edi
xorl %esi, %esi
xorl %eax, %eax
callq 0x5eb7
xorl %eax, %eax
callq 0x7bf4
movl $0x2, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1287(%rip), %rdi      # 0x97e5
callq 0x2080
movl $0x1, %edi
callq 0x2260