// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movl %r8d, %ebp
movq %rcx, %r15
movq %rdx, 0x8(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %r12
testb $0x1, %bpl
jne 0x11ee5
movq 0xc8(%r12), %rsi
testq %rsi, %rsi
je 0x11ee5
leaq 0x48(%rsi), %rax
jmp 0x11ef2
leaq 0x68(%r12), %rsi
leaq 0xb0(%r12), %rax
movq 0x280(%r12), %rcx
movq %rcx, 0x30(%rsp)
leaq 0x2c(%rsp), %rbx
movq %r12, %rdi
movq 0x18(%rsp), %rdx
movq %rbx, %rcx
callq *(%rax)
movq %rax, %r13
cmpl $0x0, (%rbx)
je 0x11f9d
cmpq $0x0, 0x8(%rsp)
je 0x11fe4
movq 0x280(%r12), %rdi
leaq 0x428b(%rip), %r14      # 0x161c3
movl $0x30, %esi
movq %r14, %rdx
movl $0x825, %ecx            # imm = 0x825
callq 0x44d0
movq %rax, %rbx
movabsq $0x800000000, %rax      # imm = 0x800000000
movq %rax, (%rbx)
movl $0x0, 0x8(%rbx)
movb $0x0, 0x28(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbx)
movq %rbx, 0x18(%r13)
movl $0x1, 0x20(%rbx)
movl $0x50, %esi
movq 0x30(%rsp), %rdi
movq %r14, %rdx
movl $0x182, %ecx            # imm = 0x182
callq 0x44d0
movq %rax, 0x28(%rbx)
movq $0x0, 0x10(%rsp)
jmp 0x1201a
movq 0x18(%r13), %rbx
testb $0x2, 0x20(%rbx)
je 0x11faf
movq 0x28(%rbx), %r13
movq 0x18(%r13), %rbx
cmpq $0x0, 0x8(%rsp)
je 0x11fe4
movl 0x20(%rbx), %eax
testb $0x4, %al
jne 0x11fef
movq %rbx, 0x10(%rsp)
testb $0x1, %al
jne 0x12024
btl $0x9, %ebp
jae 0x122ec
leaq 0x2458(%rip), %rcx      # 0x14430
leaq 0x43d1(%rip), %r8       # 0x163b0
jmp 0x122da
movq $0x0, 0x10(%rsp)
jmp 0x1203b
movl $0x1, 0x20(%rbx)
leaq 0x41c6(%rip), %rdx      # 0x161c3
movl $0x50, %esi
movq 0x30(%rsp), %rdi
movl $0x188, %ecx            # imm = 0x188
callq 0x44d0
movq %rax, 0x28(%rbx)
movq %rbx, 0x10(%rsp)
movq %rax, %rdi
xorl %esi, %esi
callq 0x45a0
movq 0x28(%rbx), %rsi
leaq 0x2c(%rsp), %rcx
movq %r12, %rdi
movq 0x8(%rsp), %rdx
callq *0x48(%rsi)
movq %rax, %r13
testb $0x4, %bpl
jne 0x1204e
movq %r15, %rdi
callq 0x41d0
movq %rax, %rbx
jmp 0x1205f
leaq 0x3c(%rsp), %rsi
movq %r15, %rdi
callq 0x4110
movl %eax, %ebx
incl %ebx
cmpl $0x0, 0x2c(%rsp)
movl %ebp, 0x4(%rsp)
je 0x12287
movq %r15, 0x20(%rsp)
movq %r12, %rbp
movq 0x280(%r12), %rdi
leal -0x8(%rbx), %esi
xorl %r12d, %r12d
testl %esi, %esi
cmovlel %r12d, %esi
movq %r13, %r15
movl $0x8, %r13d
cmovnsl %ebx, %r13d
addl $0x30, %esi
leaq 0x4122(%rip), %rdx      # 0x161c3
movl $0x825, %ecx            # imm = 0x825
callq 0x44d0
movq %rax, %r14
movl %r12d, (%rax)
movl %r13d, 0x4(%rax)
movq %r15, %r13
movl %r12d, 0x8(%rax)
movb $0x0, 0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movl %r12d, 0x20(%rax)
movq %rax, 0x18(%r15)
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x120f1
cmpq $0x0, 0x18(%rsi)
je 0x120f1
movq %rbp, %r12
movq 0x280(%rbp), %rdi
callq 0x12301
jmp 0x120f4
movq %rbp, %r12
movl 0x4(%rsp), %ebp
movq 0x20(%rsp), %r15
movl (%r14), %ecx
addl %ebx, %ecx
movl 0x4(%r14), %eax
cmpl %eax, %ecx
jl 0x121dd
leal (%rax,%rax), %edx
movq %r13, 0x40(%rsp)
xorl %r13d, %r13d
cmpl %ecx, %edx
movl %ebx, %ecx
cmovgl %r13d, %ecx
leal (%rcx,%rax,2), %edx
movq 0x280(%r12), %rdi
leal (%rcx,%rax,2), %esi
addl $-0x8, %esi
testl %esi, %esi
cmovlel %r13d, %esi
movq %r15, %rbp
movl $0x8, %r15d
cmovnsl %edx, %r15d
addl $0x30, %esi
leaq 0x4074(%rip), %rdx      # 0x161c3
movl $0x825, %ecx            # imm = 0x825
callq 0x44d0
movq %r12, 0x20(%rsp)
movq %rax, %r12
movl %r13d, (%rax)
movl %r15d, 0x4(%rax)
movq %rbp, %r15
movl 0x4(%rsp), %ebp
movl %r13d, 0x8(%rax)
leaq 0x28(%rax), %rdi
movb $0x0, 0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movl %r13d, 0x20(%rax)
movq 0x40(%rsp), %r13
movl (%r14), %eax
movl %eax, (%r12)
movl 0x8(%r14), %eax
movl %eax, 0x8(%r12)
movups 0x10(%r14), %xmm0
movups %xmm0, 0x10(%r12)
movl 0x20(%r14), %eax
movl %eax, 0x20(%r12)
leaq 0x28(%r14), %rsi
callq 0x40f0
movq %r12, 0x18(%r13)
movq 0x30(%rsp), %rdi
movq %r14, %rsi
leaq 0x3ff8(%rip), %rdx      # 0x161c3
movl $0x1d1, %ecx            # imm = 0x1D1
callq 0x4280
movq %r12, %r14
movq 0x20(%rsp), %r12
testb $0x4, %bpl
jne 0x121fb
movslq (%r14), %rax
leaq (%r14,%rax), %rdi
addq $0x28, %rdi
movq %r15, %rsi
callq 0x40f0
addl %ebx, (%r14)
jmp 0x1223a
movl (%r14), %eax
testb $0x8, %bpl
jne 0x12213
testl %eax, %eax
jle 0x12213
movb $0x20, 0x28(%r14,%rax)
incl %eax
movl %eax, (%r14)
cltq
leaq (%r14,%rax), %rsi
addq $0x28, %rsi
movl 0x3c(%rsp), %edx
movq %r15, %rdi
callq 0x4170
movslq (%r14), %rcx
cltq
addq %rcx, %rax
movl %eax, (%r14)
movb $0x0, 0x28(%r14,%rax)
andb $-0x5, 0x20(%r14)
andl $0x1, %ebp
cmpq $0x0, 0x10(%r14)
movq 0x10(%rsp), %rsi
je 0x122ae
orl $0x20, %ebp
movq %r12, %rdi
movq %r13, %rdx
movq 0x18(%rsp), %rcx
movq 0x8(%rsp), %r8
movl %ebp, %r9d
callq 0x11d13
testq %rax, %rax
je 0x122bc
leaq 0x21bb(%rip), %rcx      # 0x14430
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %rax, %r8
jmp 0x122e7
movq 0x18(%r13), %r14
movl 0x20(%r14), %eax
testb $0x1, %al
jne 0x122c6
testb $0x2, %bpl
je 0x122a2
andl $0x4, %eax
je 0x120fd
movl $0x0, (%r14)
jmp 0x120fd
testq %rsi, %rsi
je 0x122c0
cmpq $0x0, 0x10(%rsi)
jne 0x1224e
jmp 0x122c0
movq 0x18(%r13), %r14
addq $0x28, %r14
jmp 0x122ef
btl $0x9, %ebp
jae 0x122ec
leaq 0x215d(%rip), %rcx      # 0x14430
leaq 0x4104(%rip), %r8       # 0x163de
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x11c93
xorl %r14d, %r14d
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq