// File: /workspace/github_c_makefile_star2_O3/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 0x15457
leaq 0x627b(%rip), %rdi      # 0x1b6bf
callq 0x3040
movl $0x1, %ebp
testq %rax, %rax
jne 0x155e5
leaq 0x12e82(%rip), %r15     # 0x282e0
leaq 0x4157(%rip), %rdx      # 0x195bc
leaq 0x6b53(%rip), %rcx      # 0x1bfbf
xorl %ebp, %ebp
movq %r15, %rdi
movl $0x10000, %esi          # imm = 0x10000
xorl %eax, %eax
callq 0xa0ca
testl %r14d, %r14d
jle 0x15589
movq %rbx, 0x10(%rsp)
movslq %eax, %r12
addq %r15, %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 0x15566
movb $0x26, (%r12)
leaq 0x1(%r12), %rdi
leaq 0x12df6(%rip), %r15     # 0x282e0
movl %r15d, %esi
subl %edi, %esi
addl $0x10000, %esi          # imm = 0x10000
leaq 0x40c0(%rip), %rdx      # 0x195bc
movq %rbx, %rcx
xorl %eax, %eax
callq 0xa0ca
cltq
addq %rax, %r12
incq %r12
testq %r13, %r13
jne 0x15535
movl %r15d, %esi
subl %r12d, %esi
addl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdi
leaq 0x6aab(%rip), %rdx      # 0x1bfd4
xorl %eax, %eax
callq 0xa0ca
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 0x1556d
incl %ebp
movl 0x4(%rsp), %r14d
cmpl %r14d, %ebp
jl 0x1549a
movl (%rsp), %ebp
testl %ebp, %ebp
je 0x15587
movq 0x10(%rsp), %rax
movl %r14d, (%rax)
jmp 0x15589
xorl %ebp, %ebp
leaq 0x6a48(%rip), %rdi      # 0x1bfd8
callq 0x3420
leaq 0x12d44(%rip), %rdi     # 0x282e0
callq 0x3420
leaq 0x58df(%rip), %rdi      # 0x1ae87
callq 0x3040
testq %rax, %rax
leaq 0x58b9(%rip), %rcx      # 0x1ae70
cmovneq %rax, %rcx
leaq 0x22d1e(%rip), %rbx     # 0x382e0
leaq 0x6a22(%rip), %rdx      # 0x1bfeb
movq %rbx, %rdi
movl $0x200, %esi            # imm = 0x200
xorl %eax, %eax
callq 0xa0ca
movq %rbx, %rdi
callq 0x3420
callq 0x1587c
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq