// File: /workspace/github_c_makefile_star2_O2/haxlang[P]hax/haxVar.c
// Function: Hax_SetVar2
char *
Hax_SetVar2(
    Hax_Interp *interp,		/* Command interpreter in which variable is
				 * to be looked up. */
    char *part1,		/* If part2 is NULL, this is name of scalar
				 * variable.  Otherwise it is name of array. */
    char *part2,		/* Name of an element within array, or NULL. */
    char *newValue,		/* New value for variable. */
    int flags			/* Various flags that tell how to set value:
				 * any of HAX_GLOBAL_ONLY, HAX_APPEND_VALUE,
				 * HAX_LIST_ELEMENT, and HAX_NO_SPACE, or
				 * HAX_LEAVE_ERR_MSG . */)
{
    Hax_HashEntry *hPtr;
    Var *varPtr = NULL;
				/* Initial value only used to stop compiler
				 * from complaining; not really needed. */
    Interp *iPtr = (Interp *) interp;
    Hax_Memoryp *memoryp = iPtr->memoryp;
    int length, newPtr, listFlags;
    Var *arrayPtr = NULL;

    /*
     * Lookup the first name.
     */

    if ((flags & HAX_GLOBAL_ONLY) || (iPtr->varFramePtr == NULL)) {
	hPtr = Hax_CreateHashEntry(interp, &iPtr->globalTable, part1, &newPtr);
    } else {
	hPtr = Hax_CreateHashEntry(interp, &iPtr->varFramePtr->varTable,
		part1, &newPtr);
    }
    if (!newPtr) {
	varPtr = (Var *) Hax_GetHashValue(hPtr);
	if (varPtr->flags & VAR_UPVAR) {
	    hPtr = varPtr->value.upvarPtr;
	    varPtr = (Var *) Hax_GetHashValue(hPtr);
	}
    }

    /*
     * If this is an array reference, then create a new array (if
     * needed), remember any traces on the array, and lookup the
     * element within the array.
     */

    if (part2 != NULL) {
	if (newPtr) {
	    varPtr = NewVar(interp, 0);
	    Hax_SetHashValue(hPtr, varPtr);
	    varPtr->flags = VAR_ARRAY;
	    varPtr->value.tablePtr = (Hax_HashTable *)
		    ckalloc(memoryp, sizeof(Hax_HashTable));
	    Hax_InitHashTable(varPtr->value.tablePtr, HAX_STRING_KEYS);
	} else {
	    if (varPtr->flags & VAR_UNDEFINED) {
		varPtr->flags = VAR_ARRAY;
		varPtr->value.tablePtr = (Hax_HashTable *)
			ckalloc(memoryp, sizeof(Hax_HashTable));
		Hax_InitHashTable(varPtr->value.tablePtr, HAX_STRING_KEYS);
	    } else if (!(varPtr->flags & VAR_ARRAY)) {
		if (flags & HAX_LEAVE_ERR_MSG) {
		    VarErrMsg(interp, part1, part2, (char *) "set",
			(char *) needArray);
		}
		return NULL;
	    }
	    arrayPtr = varPtr;
	}
	hPtr = Hax_CreateHashEntry(interp, varPtr->value.tablePtr, part2,
	                           &newPtr);
    }

    /*
     * Compute how many bytes will be needed for newValue (leave space
     * for a separating space between list elements).
     */

    if (flags & HAX_LIST_ELEMENT) {
	length = Hax_ScanElement(newValue, &listFlags) + 1;
    } else {
	length = strlen(newValue);
    }

    /*
     * If the variable doesn't exist then create a new one.  If it
     * does exist then clear its current value unless this is an
     * append operation.
     */

    if (newPtr) {
	varPtr = NewVar(interp, length);
	Hax_SetHashValue(hPtr, varPtr);
	if ((arrayPtr != NULL) && (arrayPtr->searchPtr != NULL)) {
	    DeleteSearches(interp, arrayPtr);
	}
    } else {
	varPtr = (Var *) Hax_GetHashValue(hPtr);
	if (varPtr->flags & VAR_ARRAY) {
	    if (flags & HAX_LEAVE_ERR_MSG) {
		VarErrMsg(interp, part1, part2, (char *) "set",
		    (char *) isArray);
	    }
	    return NULL;
	}
	if (!(flags & HAX_APPEND_VALUE) || (varPtr->flags & VAR_UNDEFINED)) {
	    varPtr->valueLength = 0;
	}
    }

    /*
     * Make sure there's enough space to hold the variable's
     * new value.  If not, enlarge the variable's space.
     */

    if ((length + varPtr->valueLength) >= varPtr->valueSpace) {
	Var *newVarPtr;
	int newSize;

	newSize = 2*varPtr->valueSpace;
	if (newSize <= (length + varPtr->valueLength)) {
	    newSize += length;
	}
	newVarPtr = NewVar(interp, newSize);
	newVarPtr->valueLength = varPtr->valueLength;
	newVarPtr->upvarUses = varPtr->upvarUses;
	newVarPtr->tracePtr = varPtr->tracePtr;
	newVarPtr->searchPtr = varPtr->searchPtr;
	newVarPtr->flags = varPtr->flags;
	strcpy(newVarPtr->value.string, varPtr->value.string);
	Hax_SetHashValue(hPtr, newVarPtr);
	ckfree(memoryp, (char *) varPtr);
	varPtr = newVarPtr;
    }

    /*
     * Append the new value to the variable, either as a list
     * element or as a string.
     */

    if (flags & HAX_LIST_ELEMENT) {
	if ((varPtr->valueLength > 0) && !(flags & HAX_NO_SPACE)) {
	    ((char *)varPtr->value.string)[varPtr->valueLength] = ' ';
	    varPtr->valueLength++;
	}
	varPtr->valueLength += Hax_ConvertElement(newValue,
		(char *)varPtr->value.string + varPtr->valueLength, listFlags);
	((char *)varPtr->value.string)[varPtr->valueLength] = 0;
    } else {
	strcpy(((char *)varPtr->value.string) + varPtr->valueLength, newValue);
	varPtr->valueLength += length;
    }
    varPtr->flags &= ~VAR_UNDEFINED;

    /*
     * Invoke any write traces for the variable.
     */

    if ((varPtr->tracePtr != NULL)
	    || ((arrayPtr != NULL) && (arrayPtr->tracePtr != NULL))) {
	char *msg;

	msg = CallTraces(iPtr, arrayPtr, hPtr, part1, part2,
		(flags & HAX_GLOBAL_ONLY) | HAX_TRACE_WRITES);
	if (msg != NULL) {
	    VarErrMsg(interp, part1, part2, (char *) "set", (char *) msg);
	    return NULL;
	}

	/*
	 * Watch out!  The variable could have gotten re-allocated to
	 * a larger size.  Fortunately the hash table entry will still
	 * be around.
	 */

	varPtr = (Var *) Hax_GetHashValue(hPtr);
    }
    return varPtr->value.string;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r8d, %ebp
movq %rcx, 0x10(%rsp)
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %r15
testb $0x1, %bpl
jne 0x1107c
movq 0xc8(%r15), %rsi
testq %rsi, %rsi
je 0x1107c
leaq 0x48(%rsi), %rax
jmp 0x11087
leaq 0x68(%r15), %rsi
leaq 0xb0(%r15), %rax
movq 0x280(%r15), %rcx
movq %rcx, 0x28(%rsp)
leaq 0xc(%rsp), %rbx
movq %r15, %rdi
movq %r12, %rdx
movq %rbx, %rcx
callq *(%rax)
movq %rax, %r14
cmpl $0x0, (%rbx)
je 0x110d0
testq %r13, %r13
je 0x1111e
movq $0x0, (%rsp)
movq %r15, %rdi
xorl %esi, %esi
callq 0x11371
movq %rax, %rbx
movq %rax, 0x18(%r14)
movl $0x182, %ecx            # imm = 0x182
jmp 0x1112c
movq 0x18(%r14), %rbx
testb $0x2, 0x20(%rbx)
je 0x110e2
movq 0x28(%rbx), %r14
movq 0x18(%r14), %rbx
testq %r13, %r13
je 0x1111e
movl 0x20(%rbx), %eax
movl $0x188, %ecx            # imm = 0x188
testb $0x4, %al
jne 0x11128
movq %rbx, (%rsp)
testb $0x1, %al
jne 0x11155
btl $0x9, %ebp
jae 0x1135c
leaq 0x2324(%rip), %rcx      # 0x13430
leaq 0x426d(%rip), %r8       # 0x15380
movq %r15, %rdi
movq %r12, %rsi
jmp 0x11354
movq $0x0, (%rsp)
jmp 0x1116a
movq %rbx, (%rsp)
movl $0x1, 0x20(%rbx)
leaq 0x4059(%rip), %rdx      # 0x15193
pushq $0x50
popq %rsi
movq 0x28(%rsp), %rdi
callq 0x44e0
movq %rax, 0x28(%rbx)
movq %rax, %rdi
xorl %esi, %esi
callq 0x45b0
movq 0x28(%rbx), %rsi
leaq 0xc(%rsp), %rcx
movq %r15, %rdi
movq %r13, %rdx
callq *0x48(%rsi)
movq %rax, %r14
testb $0x4, %bpl
jne 0x1117f
movq 0x10(%rsp), %rdi
callq 0x41d0
movq %rax, %rbx
jmp 0x11192
leaq 0x1c(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x4110
movl %eax, %ebx
incl %ebx
movq %r12, 0x20(%rsp)
cmpl $0x0, 0xc(%rsp)
je 0x111c9
movq %r15, %rdi
movl %ebx, %esi
callq 0x11371
movq %rax, %r12
movq %rax, 0x18(%r14)
movq (%rsp), %rsi
testq %rsi, %rsi
je 0x111ea
cmpq $0x0, 0x18(%rsi)
je 0x111ea
movq %r15, %rdi
callq 0x113bb
jmp 0x111ea
movq 0x18(%r14), %r12
movl 0x20(%r12), %eax
testb $0x1, %al
jne 0x11338
testb $0x2, %bpl
je 0x111e5
andl $0x4, %eax
je 0x111ea
andl $0x0, (%r12)
movl (%r12), %ecx
addl %ebx, %ecx
movl 0x4(%r12), %eax
cmpl %eax, %ecx
jl 0x1126e
leal (%rax,%rax), %edx
xorl %esi, %esi
cmpl %ecx, %edx
cmovlel %ebx, %esi
leal (%rsi,%rax,2), %esi
movq %r15, %rdi
callq 0x11371
movq %r13, 0x30(%rsp)
movq %rax, %r13
movl (%r12), %eax
movl %eax, (%r13)
movl 0x8(%r12), %eax
movl %eax, 0x8(%r13)
movups 0x10(%r12), %xmm0
movups %xmm0, 0x10(%r13)
movl 0x20(%r12), %eax
movl %eax, 0x20(%r13)
leaq 0x28(%r13), %rdi
leaq 0x28(%r12), %rsi
callq 0x40f0
movq %r13, 0x18(%r14)
leaq 0x3f3f(%rip), %rdx      # 0x15193
movq 0x28(%rsp), %rdi
movq %r12, %rsi
movl $0x1d1, %ecx            # imm = 0x1D1
callq 0x4280
movq %r13, %r12
movq 0x30(%rsp), %r13
testb $0x4, %bpl
jne 0x11290
movslq (%r12), %rax
leaq (%r12,%rax), %rdi
addq $0x28, %rdi
movq 0x10(%rsp), %rsi
callq 0x40f0
addl %ebx, (%r12)
jmp 0x112d5
movl (%r12), %eax
testb $0x8, %bpl
jne 0x112aa
testl %eax, %eax
jle 0x112aa
movb $0x20, 0x28(%r12,%rax)
incl %eax
movl %eax, (%r12)
cltq
leaq (%r12,%rax), %rsi
addq $0x28, %rsi
movl 0x1c(%rsp), %edx
movq 0x10(%rsp), %rdi
callq 0x4170
movslq (%r12), %rcx
cltq
addq %rcx, %rax
movl %eax, (%r12)
movb $0x0, 0x28(%r12,%rax)
andb $-0x5, 0x20(%r12)
cmpq $0x0, 0x10(%r12)
movq (%rsp), %rsi
je 0x11320
andl $0x1, %ebp
orl $0x20, %ebp
movq %r15, %rdi
movq %r14, %rdx
movq 0x20(%rsp), %rbx
movq %rbx, %rcx
movq %r13, %r8
movl %ebp, %r9d
callq 0x10e7b
testq %rax, %rax
je 0x1132e
leaq 0x211e(%rip), %rcx      # 0x13430
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %rax, %r8
jmp 0x11357
testq %rsi, %rsi
je 0x11332
cmpq $0x0, 0x10(%rsi)
jne 0x112e7
jmp 0x11332
movq 0x18(%r14), %r12
addq $0x28, %r12
jmp 0x1135f
btl $0x9, %ebp
jae 0x1135c
leaq 0x20eb(%rip), %rcx      # 0x13430
leaq 0x4062(%rip), %r8       # 0x153ae
movq %r15, %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rdx
callq 0x10dfb
xorl %r12d, %r12d
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq