// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
movq %rsi, 0x8(%rsp)
movq %rdi, %rbx
movl (%rdi), %r14d
testl %edx, %edx
je 0x156c0
leaq 0x6005(%rip), %rdi      # 0x1b6b2
callq 0x3040
movl $0x1, %ebp
testq %rax, %rax
jne 0x15851
movq %rbx, 0x10(%rsp)
leaq 0x12c14(%rip), %rbx     # 0x282e0
leaq 0x3ed9(%rip), %rdx      # 0x195ac
leaq 0x68d8(%rip), %rcx      # 0x1bfb2
movq %rbx, %rdi
movl $0x10000, %esi          # imm = 0x10000
xorl %eax, %eax
callq 0xa2ce
testl %r14d, %r14d
jle 0x157df
movslq %eax, %r12
addq %rbx, %r12
xorl %ebp, %ebp
movl $0x0, (%rsp)
movl %r14d, 0x4(%rsp)
movslq %ebp, %r14
movq 0x8(%rsp), %rax
movq (%rax,%r14,8), %rax
xorl %ebx, %ebx
cmpb $0x2d, (%rax)
setne %r15b
sete %bl
addq %rax, %rbx
movq %rbx, %rdi
movl $0x3d, %esi
callq 0x3260
movq %rax, %r13
testq %rax, %rax
sete %al
testb %al, %r15b
jne 0x157cd
movb $0x26, (%r12)
leaq 0x1(%r12), %rdi
leaq 0x12b8f(%rip), %r15     # 0x282e0
movl %r15d, %esi
subl %edi, %esi
addl $0x10000, %esi          # imm = 0x10000
leaq 0x3e49(%rip), %rdx      # 0x195ac
movq %rbx, %rcx
xorl %eax, %eax
callq 0xa2ce
cltq
addq %rax, %r12
incq %r12
testq %r13, %r13
jne 0x1579c
movl %r15d, %esi
subl %r12d, %esi
addl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdi
leaq 0x6837(%rip), %rdx      # 0x1bfc7
xorl %eax, %eax
callq 0xa2ce
cltq
addq %rax, %r12
movq 0x8(%rsp), %rax
leaq (%rax,%r14,8), %rdi
leaq 0x8(%rdi), %rsi
movl %ebp, %eax
notl %eax
movl 0x4(%rsp), %r14d
addl %r14d, %eax
movslq %eax, %rdx
shlq $0x3, %rdx
callq 0x34a0
decl %r14d
movl $0x1, (%rsp)
jmp 0x157d4
incl %ebp
movl 0x4(%rsp), %r14d
cmpl %r14d, %ebp
jl 0x15701
jmp 0x157e6
movl $0x0, (%rsp)
movl (%rsp), %ebp
testl %ebp, %ebp
je 0x157f5
movq 0x10(%rsp), %rax
movl %r14d, (%rax)
leaq 0x67cf(%rip), %rdi      # 0x1bfcb
callq 0x3420
leaq 0x12ad8(%rip), %rdi     # 0x282e0
callq 0x3420
leaq 0x5666(%rip), %rdi      # 0x1ae7a
callq 0x3040
testq %rax, %rax
leaq 0x5640(%rip), %rcx      # 0x1ae63
cmovneq %rax, %rcx
leaq 0x22ab2(%rip), %rbx     # 0x382e0
leaq 0x67a9(%rip), %rdx      # 0x1bfde
movq %rbx, %rdi
movl $0x200, %esi            # imm = 0x200
xorl %eax, %eax
callq 0xa2ce
movq %rbx, %rdi
callq 0x3420
callq 0x15afe
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq