// File: /workspace/github_c_makefile_star2_O1/icebert[P]pblat/lib/cheapcgi.c
// Function: cgiMakeSelectDropList
char *cgiMakeSelectDropList(boolean multiple, char *name, struct slPair *valsAndLabels,
                            char *selected, char *anyAll,char *extraClasses, char *event, char *javascript, char *style, char *id)
// 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
// event: click, etc.
// javacript: what to execute when the event happens.
// style: inline style
// id is optional
{
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'");

char *autoId = NULL;
if (javascript)
    {
    if (!id)
	{
    	id = name;
	}
    }
if (id)
    dyStringPrintf(output, " id='%s'", id);
if (javascript)
    {
    jsOnEventById(event, id, javascript);
    freeMem(autoId);
    }

if (style)
    {
    dyStringPrintf(output, " style='%s'", style);
    }

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, %r13
movq %r8, %rbp
movq %rcx, 0x10(%rsp)
movq %rdx, %r14
movq %rsi, %r12
movl %edi, %ebx
movl $0x400, %edi            # imm = 0x400
callq 0x261d0
movq %rax, (%rsp)
leaq 0x4fc32(%rip), %rsi     # 0x963a5
movq %rax, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2656a
movl %ebx, 0xc(%rsp)
testl %ebx, %ebx
je 0x46798
movq (%rsp), %rdi
leaq 0x4fc24(%rip), %rsi     # 0x963b7
callq 0x26425
movq 0x68(%rsp), %rbx
movq 0x58(%rsp), %r15
testq %r13, %r13
je 0x467d5
cmpl $0x0, 0xc(%rsp)
movq (%rsp), %rdi
leaq 0x51ba9(%rip), %rax     # 0x98360
leaq 0x4fc11(%rip), %rcx     # 0x963cf
cmoveq %rax, %rcx
leaq 0x4fbf8(%rip), %rsi     # 0x963c1
movq %r13, %rdx
xorl %eax, %eax
callq 0x2656a
jmp 0x467ec
cmpl $0x0, 0xc(%rsp)
je 0x467ec
movq (%rsp), %rdi
leaq 0x4fbf2(%rip), %rsi     # 0x963d9
callq 0x26425
testq %rbx, %rbx
cmovneq %rbx, %r12
testq %r15, %r15
cmoveq %rbx, %r12
testq %r12, %r12
je 0x46814
movq (%rsp), %rdi
leaq 0x4f7f6(%rip), %rsi     # 0x96000
movq %r12, %rdx
xorl %eax, %eax
callq 0x2656a
movq 0x60(%rsp), %rbx
testq %r15, %r15
je 0x46849
movq 0x50(%rsp), %r13
movq %r13, %rdi
callq 0x43e34
leaq 0x4f2e1(%rip), %rdi     # 0x95b13
movq %r12, %rsi
movq %r13, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x43ca9
xorl %edi, %edi
callq 0x2dd41
testq %rbx, %rbx
je 0x46863
movq (%rsp), %rdi
leaq 0x4fa19(%rip), %rsi     # 0x96272
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2656a
movq (%rsp), %rdi
leaq 0x50b44(%rip), %rsi     # 0x973b2
callq 0x26425
testq %rbp, %rbp
je 0x4693a
movq %rbp, %rdi
movl $0x2c, %esi
callq 0x6270
testq %rax, %rax
je 0x468b1
movq %rbp, %rdi
callq 0x1f9f6
movq %rax, %rbp
movq %rax, %rdi
movl $0x2c, %esi
callq 0x6270
movq %rax, %r12
movb $0x0, (%rax)
incq %r12
jmp 0x468b4
movq %rbp, %r12
cmpq $0x0, 0x10(%rsp)
je 0x468dd
cmpl $0x0, 0xc(%rsp)
je 0x468e1
movq %rbp, %rdi
movl $0x2c, %esi
movq 0x10(%rsp), %rdx
callq 0x218ef
testq %rax, %rax
setne %al
jmp 0x468f3
movb $0x1, %al
jmp 0x468f3
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
callq 0x6a50
testl %eax, %eax
sete %al
movq (%rsp), %r13
leaq 0x4f980(%rip), %rcx     # 0x9627e
leaq 0x51a5b(%rip), %r15     # 0x98360
testb %al, %al
cmovneq %rcx, %r15
movq %r12, %rdi
callq 0x44c11
leaq 0x4fad1(%rip), %rsi     # 0x963eb
movq %r13, %rdi
movq %r15, %rdx
movq %rbp, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x2656a
cmpq %rbp, %r12
je 0x4693a
movq %rbp, %rdi
callq 0x2dd41
testq %r14, %r14
je 0x469d8
leaq 0x51a16(%rip), %rbx     # 0x98360
leaq 0x4fa9a(%rip), %r15     # 0x963eb
cmpq $0x0, 0x10(%rsp)
je 0x4697b
movq 0x8(%r14), %rdi
cmpl $0x0, 0xc(%rsp)
je 0x4697f
movl $0x2c, %esi
movq 0x10(%rsp), %rdx
callq 0x218ef
testq %rax, %rax
setne %al
jmp 0x4698e
xorl %eax, %eax
jmp 0x4698e
movq 0x10(%rsp), %rsi
callq 0x6a50
testl %eax, %eax
sete %al
movq 0x8(%r14), %r12
movq 0x10(%r14), %rdi
testq %rdi, %rdi
cmoveq %r12, %rdi
movq (%rsp), %r13
testb %al, %al
movq %rbx, %rbp
leaq 0x4f8d1(%rip), %rax     # 0x9627e
cmovneq %rax, %rbp
callq 0x44c11
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x2656a
movq (%r14), %r14
testq %r14, %r14
jne 0x46951
movq %rsp, %rbx
movq (%rbx), %rdi
leaq 0x4f8ba(%rip), %rsi     # 0x9629f
xorl %eax, %eax
callq 0x2656a
movq %rbx, %rdi
callq 0x2622c
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq