// File: /workspace/github_c_makefile_star2_O2/JimKent[P]linearSat/lib/cheapcgi.c
// Function: cgiFromCommandLine
boolean cgiFromCommandLine(int *pArgc, char *argv[], boolean preferWeb)
/* Use the command line to set up things as if we were a CGI program.
 * User types in command line (assuming your program called cgiScript)
 * like:
 *        cgiScript nonCgiArg1 var1=value1 var2=value2 var3=value3 nonCgiArg2
 * or like
 *        cgiScript nonCgiArg1 var1=value1&var2=value2&var3=value3 nonCgiArg2
 * or even like
 *        cgiScript nonCgiArg1 -x -y=bogus z=really
 * (The non-cgi arguments can occur anywhere.  The cgi arguments (all containing
 * the character '=' or starting with '-') are erased from argc/argv.  Normally
 * you call this cgiSpoof(&argc, argv);
 */
{
int argc = *pArgc;
int i;
int argcLeft = argc;
char *name;
static char queryString[65536];
char *q = queryString;
boolean needAnd = TRUE;
boolean gotAny = FALSE;
boolean startDash;
boolean gotEq;
static char hostLine[512];

if (preferWeb && cgiIsOnWeb())
    return TRUE;	/* No spoofing required! */
q += safef(q, queryString + sizeof(queryString) - q,
	   "%s", "QUERY_STRING=cgiSpoof=on");
for (i=0; i<argcLeft; )
    {
    name = argv[i];
    if ((startDash = (name[0] == '-')))
       ++name;
    gotEq = (strchr(name, '=') != NULL);
    if (gotEq || startDash)
        {
        if (needAnd)
            *q++ = '&';
        q += safef(q, queryString + sizeof(queryString) - q, "%s", name);
        if (!gotEq || strchr(name, '&') == NULL)
            needAnd = TRUE;
	if (!gotEq)
	    q += safef(q, queryString + sizeof(queryString) - q, "=on");
        memcpy(&argv[i], &argv[i+1], sizeof(argv[i]) * (argcLeft-i-1));
        argcLeft -= 1;
        gotAny = TRUE;
        }
    else
        i++;
    }
if (gotAny)
    {
    *pArgc = argcLeft;
    }
putenv("REQUEST_METHOD=GET");
putenv(queryString);
char *host = getenv("HOST");
if (host == NULL)
    host = "unknown";
safef(hostLine, sizeof(hostLine), "SERVER_NAME=%s", host);
putenv(hostLine);
initCgiInput();
return gotAny;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
movl (%rdi), %eax
movq %rax, (%rsp)
testl %edx, %edx
je 0x13882
callq 0x1151b
testl %eax, %eax
je 0x13882
pushq $0x1
popq %rax
jmp 0x13a29
movq %rbx, 0x20(%rsp)
leaq 0x13a72(%rip), %r12     # 0x27300
leaq 0x3d27(%rip), %rdx      # 0x175bc
leaq 0x6723(%rip), %rcx      # 0x19fbf
movq %r12, %rdi
movl $0x10000, %esi          # imm = 0x10000
xorl %eax, %eax
callq 0x939d
movslq %eax, %r14
addq %r12, %r14
movq (%rsp), %rax
leal -0x1(%rax), %ebx
movb $0x1, %al
movl %eax, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
xorl %r15d, %r15d
movslq %r15d, %r15
movq %rbx, 0x10(%rsp)
cmpl (%rsp), %r15d
jge 0x139b7
movq 0x18(%rsp), %rax
movq %r15, %r13
movq (%rax,%r15,8), %rax
movb (%rax), %bpl
xorl %r15d, %r15d
cmpb $0x2d, %bpl
sete %r15b
addq %rax, %r15
movq %r15, %rdi
pushq $0x3d
popq %rsi
callq 0x3260
movq %rax, %rbx
cmpb $0x2d, %bpl
je 0x13919
testq %rbx, %rbx
jne 0x13919
movq %r13, %r15
incq %r15
jmp 0x138d1
movb $0x26, (%r14)
leaq 0x1(%r14), %rdi
movl %r12d, %esi
subl %edi, %esi
addl $0x10000, %esi          # imm = 0x10000
leaq 0x3c89(%rip), %rdx      # 0x175bc
movq %r15, %rcx
xorl %eax, %eax
callq 0x939d
cltq
addq %rax, %r14
incq %r14
testq %rbx, %rbx
jne 0x1396c
movl %r12d, %esi
subl %r14d, %esi
addl $0x10000, %esi          # imm = 0x10000
movq %r14, %rdi
leaq 0x6674(%rip), %rdx      # 0x19fd4
xorl %eax, %eax
callq 0x939d
cltq
addq %rax, %r14
movq 0x10(%rsp), %rbx
movq %r13, %r15
movq 0x18(%rsp), %rax
leaq (%rax,%r13,8), %rdi
leaq (%rax,%r13,8), %rsi
addq $0x8, %rsi
movl %ebx, %eax
subl %r15d, %eax
movslq %eax, %rdx
shlq $0x3, %rdx
callq 0x34a0
movq (%rsp), %rax
decl %eax
movq %rax, (%rsp)
decq %rbx
movl $0x0, 0x8(%rsp)
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
jmp 0x138c9
testb $0x1, 0x8(%rsp)
jne 0x139c9
movq 0x20(%rsp), %rax
movq (%rsp), %rcx
movl %ecx, (%rax)
leaq 0x6608(%rip), %rdi      # 0x19fd8
callq 0x3420
leaq 0x13924(%rip), %rdi     # 0x27300
callq 0x3420
leaq 0x549f(%rip), %rdi      # 0x18e87
callq 0x3040
testq %rax, %rax
leaq 0x5479(%rip), %rcx      # 0x18e70
cmovneq %rax, %rcx
leaq 0x238fe(%rip), %rbx     # 0x37300
leaq 0x65e2(%rip), %rdx      # 0x19feb
movq %rbx, %rdi
movl $0x200, %esi            # imm = 0x200
xorl %eax, %eax
callq 0x939d
movq %rbx, %rdi
callq 0x3420
callq 0x13d67
movl 0xc(%rsp), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq