// File: /workspace/github_c_makefile_star2_O1/JimKent[P]linearSat/lib/cheapcgi.c
// Function: cgiMakeSelectDropList
char *cgiMakeSelectDropList(boolean multiple, char *name, struct slPair *valsAndLabels,
                            char *selected, char *anyAll,char *extraClasses, char *extraHtml)
// Returns allocated string of HTML defining a drop-down select
// (if multiple, REQUIRES ui-dropdownchecklist.js)
// valsAndLabels: val (pair->name) must be filled in but label (pair->val) may be NULL.
// selected: if not NULL is a val found in the valsAndLabels (multiple then comma delimited list).
//           If null and anyAll not NULL, that will be selected
// anyAll: if not NULL is the string for an initial option. It can contain val and label,
//         delimited by a comma
// extraHtml: if not NULL contains id, javascript calls and style.
//            It does NOT contain class definitions
{
struct dyString *output = dyStringNew(1024);
boolean checked = FALSE;

dyStringPrintf(output,"<SELECT name='%s'",name);
if (multiple)
    dyStringAppend(output," MULTIPLE");
if (extraClasses != NULL)
    dyStringPrintf(output," class='%s%s'",extraClasses,(multiple ? " filterBy" : ""));
else if (multiple)
    dyStringAppend(output," class='filterBy'");

if (extraHtml != NULL)
    dyStringPrintf(output," %s",extraHtml);
dyStringAppend(output,">\n");

// Handle initial option "Any" or "All"
if (anyAll != NULL)
    {
    char *val = anyAll;  // Could contain a label after the value
    char *label = strchr(val,',');  // Could contain a label after the value
    if (label != NULL)
        {
        val = cloneString(anyAll);
        label = strchr(val,',');  // again because this is new mem
        *label = '\0';
        label = label+1;
        }
    else
        label = val;
    checked = TRUE; // The default case
    if (selected != NULL)
        {
        if (multiple)
            checked = (findWordByDelimiter(val,',', selected) != NULL);
        else
            checked = sameString(val,selected);
        }
    dyStringPrintf(output, "<OPTION%s VALUE='%s'>%s</OPTION>\n",(checked ? " SELECTED" : ""),
                   val, javaScriptLiteralEncode(label));
    if (label != val)
        freeMem(val);
    }

// All other options
struct slPair *valPair = valsAndLabels;
for (; valPair != NULL; valPair = valPair->next)
    {
    checked = FALSE;
    if (selected != NULL)
        {
        if (multiple)
            checked = (findWordByDelimiter(valPair->name,',', selected) != NULL);
        else
            checked = sameString(valPair->name,selected);
        }
    char *label = valPair->name;
    if (valPair->val != NULL)
        label = valPair->val;
    dyStringPrintf(output, "<OPTION%s VALUE='%s'>%s</OPTION>\n",(checked ? " SELECTED" : ""),
                   (char *)valPair->name, javaScriptLiteralEncode(label));
    }

dyStringPrintf(output,"</SELECT>\n");

return dyStringCannibalize(&output);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, %r12
movq %r8, %r15
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r13
movl %edi, %ebp
movl $0x400, %edi            # imm = 0x400
callq 0xaf38
movq %rax, 0x10(%rsp)
leaq 0x6e61(%rip), %rsi      # 0x1be80
movq %rax, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xb2d2
movl %ebp, 0xc(%rsp)
testl %ebp, %ebp
je 0x15045
movq 0x10(%rsp), %rdi
leaq 0x6e52(%rip), %rsi      # 0x1be92
callq 0xb18d
movq 0x50(%rsp), %r13
testq %r12, %r12
je 0x1507e
cmpl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x4c62(%rip), %rax      # 0x19cc2
leaq 0x6e43(%rip), %rcx      # 0x1beaa
cmoveq %rax, %rcx
leaq 0x6e2a(%rip), %rsi      # 0x1be9c
movq %r12, %rdx
xorl %eax, %eax
callq 0xb2d2
jmp 0x15096
cmpl $0x0, 0xc(%rsp)
je 0x15096
movq 0x10(%rsp), %rdi
leaq 0x6e23(%rip), %rsi      # 0x1beb4
callq 0xb18d
testq %r13, %r13
je 0x150b1
movq 0x10(%rsp), %rdi
leaq 0x4da4(%rip), %rsi      # 0x19e4b
movq %r13, %rdx
xorl %eax, %eax
callq 0xb2d2
movq 0x10(%rsp), %rdi
leaq 0x6cf4(%rip), %rsi      # 0x1bdb1
callq 0xb18d
testq %r15, %r15
je 0x15186
movq %r15, %rdi
movl $0x2c, %esi
callq 0x3260
testq %rax, %rax
je 0x15100
movq %r15, %rdi
callq 0x647e
movq %rax, %r15
movq %rax, %rdi
movl $0x2c, %esi
callq 0x3260
movq %rax, %r12
movb $0x0, (%rax)
incq %r12
jmp 0x15103
movq %r15, %r12
testq %rbx, %rbx
je 0x15127
cmpl $0x0, 0xc(%rsp)
je 0x1512b
movq %r15, %rdi
movl $0x2c, %esi
movq %rbx, %rdx
callq 0x82ec
testq %rax, %rax
setne %al
jmp 0x1513b
movb $0x1, %al
jmp 0x1513b
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3440
testl %eax, %eax
sete %al
movq %r15, %rbp
movq 0x10(%rsp), %r13
leaq 0x6bf6(%rip), %rcx      # 0x1bd40
leaq 0x4b71(%rip), %r15      # 0x19cc2
testb %al, %al
cmovneq %rcx, %r15
movq %r12, %rdi
callq 0x13988
leaq 0x6d60(%rip), %rsi      # 0x1bec6
movq %r13, %rdi
movq %r15, %rdx
movq %rbp, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0xb2d2
cmpq %rbp, %r12
je 0x15186
movq %rbp, %rdi
callq 0xf129
testq %r14, %r14
je 0x1521b
leaq 0x6d30(%rip), %r15      # 0x1bec6
testq %rbx, %rbx
je 0x151bb
movq 0x8(%r14), %rdi
cmpl $0x0, 0xc(%rsp)
je 0x151bf
movl $0x2c, %esi
movq %rbx, %rdx
callq 0x82ec
testq %rax, %rax
setne %al
jmp 0x151cc
xorl %eax, %eax
jmp 0x151cc
movq %rbx, %rsi
callq 0x3440
testl %eax, %eax
sete %al
movq 0x8(%r14), %r12
movq 0x10(%r14), %rdi
testq %rdi, %rdi
cmoveq %r12, %rdi
movq 0x10(%rsp), %r13
testb %al, %al
leaq 0x4ad9(%rip), %rbp      # 0x19cc2
leaq 0x6b50(%rip), %rax      # 0x1bd40
cmovneq %rax, %rbp
callq 0x13988
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0xb2d2
movq (%r14), %r14
testq %r14, %r14
jne 0x15196
leaq 0x10(%rsp), %rbx
movq (%rbx), %rdi
leaq 0x6b37(%rip), %rsi      # 0x1bd61
xorl %eax, %eax
callq 0xb2d2
movq %rbx, %rdi
callq 0xaf94
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq