// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x28(%rbp)
leaq 0x143e7(%rip), %rax     # 0x33310
movq %rax, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x1ef57
callq 0x1b530
cmpl $0x0, %eax
je 0x1ef57
movl $0x1, -0x4(%rbp)
jmp 0x1f198
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rcx
leaq 0x143aa(%rip), %rax     # 0x33310
addq $0x10000, %rax          # imm = 0x10000
subq %rcx, %rax
movl %eax, %esi
leaq 0x5634(%rip), %rdx      # 0x245ac
leaq 0x80b1(%rip), %rcx      # 0x27030
movb $0x0, %al
callq 0xdc90
movl %eax, %ecx
movq -0x38(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x1f11a
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
sete %al
movb %al, %cl
andb $0x1, %cl
movzbl %cl, %ecx
movl %ecx, -0x44(%rbp)
testb $0x1, %al
jne 0x1efd7
jmp 0x1efe3
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movl $0x3d, %esi
callq 0x3250
cmpq $0x0, %rax
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jne 0x1f010
cmpl $0x0, -0x44(%rbp)
je 0x1f10c
cmpl $0x0, -0x3c(%rbp)
je 0x1f028
movq -0x38(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x38(%rbp)
movb $0x26, (%rax)
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rcx
leaq 0x142d9(%rip), %rax     # 0x33310
addq $0x10000, %rax          # imm = 0x10000
subq %rcx, %rax
movl %eax, %esi
movq -0x30(%rbp), %rcx
leaq 0x555f(%rip), %rdx      # 0x245ac
movb $0x0, %al
callq 0xdc90
movl %eax, %ecx
movq -0x38(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x1f07e
movq -0x30(%rbp), %rdi
movl $0x26, %esi
callq 0x3250
cmpq $0x0, %rax
jne 0x1f085
movl $0x1, -0x3c(%rbp)
cmpl $0x0, -0x48(%rbp)
jne 0x1f0c3
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rcx
leaq 0x14276(%rip), %rax     # 0x33310
addq $0x10000, %rax          # imm = 0x10000
subq %rcx, %rax
movl %eax, %esi
leaq 0x7f99(%rip), %rdx      # 0x27045
movb $0x0, %al
callq 0xdc90
movl %eax, %ecx
movq -0x38(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rdi
movslq -0x24(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x18(%rbp), %rsi
movl -0x24(%rbp), %eax
addl $0x1, %eax
cltq
shlq $0x3, %rax
addq %rax, %rsi
movl -0x28(%rbp), %eax
subl -0x24(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rdx
shlq $0x3, %rdx
callq 0x3470
movl -0x28(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x28(%rbp)
movl $0x1, -0x40(%rbp)
jmp 0x1f115
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1ef9d
cmpl $0x0, -0x40(%rbp)
je 0x1f129
movl -0x28(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
leaq 0x7f19(%rip), %rdi      # 0x27049
callq 0x33f0
leaq 0x141d4(%rip), %rdi     # 0x33310
callq 0x33f0
leaq 0x6d50(%rip), %rdi      # 0x25e98
callq 0x3030
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x1f163
leaq 0x6d22(%rip), %rax      # 0x25e81
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rcx
leaq 0x241a2(%rip), %rdi     # 0x43310
movl $0x200, %esi            # imm = 0x200
leaq 0x7ee2(%rip), %rdx      # 0x2705c
movb $0x0, %al
callq 0xdc90
leaq 0x24188(%rip), %rdi     # 0x43310
callq 0x33f0
callq 0x1f650
movl -0x40(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)