// File: /workspace/github_c_makefile_star2_O2/haxlang[P]hax/haxVar.c
// Function: Hax_UnsetVar2
int
Hax_UnsetVar2(
    Hax_Interp *interp,		/* Command interpreter in which varName is
				 * to be looked up. */
    char *part1,		/* Name of variable or array. */
    char *part2,		/* Name of element within array or NULL. */
    int flags			/* OR-ed combination of any of
				 * HAX_GLOBAL_ONLY or HAX_LEAVE_ERR_MSG. */)
{
    Hax_HashEntry *hPtr, dummyEntry;
    Var *varPtr, dummyVar;
    Interp *iPtr = (Interp *) interp;
    Hax_Memoryp *memoryp = iPtr->memoryp;
    Var *arrayPtr = NULL;

    if ((flags & HAX_GLOBAL_ONLY) || (iPtr->varFramePtr == NULL)) {
	hPtr = Hax_FindHashEntry(&iPtr->globalTable, part1);
    } else {
	hPtr = Hax_FindHashEntry(&iPtr->varFramePtr->varTable, part1);
    }
    if (hPtr == NULL) {
	if (flags & HAX_LEAVE_ERR_MSG) {
	    VarErrMsg(interp, part1, part2, (char *) "unset",
		(char *) noSuchVar);
	}
	return -1;
    }
    varPtr = (Var *) Hax_GetHashValue(hPtr);

    /*
     * For global variables referenced in procedures, leave the procedure's
     * reference variable in place, but unset the global variable.  Can't
     * decrement the actual variable's use count, since we didn't delete
     * the reference variable.
     */

    if (varPtr->flags & VAR_UPVAR) {
	hPtr = varPtr->value.upvarPtr;
	varPtr = (Var *) Hax_GetHashValue(hPtr);
    }

    /*
     * If the variable being deleted is an element of an array, then
     * remember trace procedures on the overall array and find the
     * element to delete.
     */

    if (part2 != NULL) {
	if (!(varPtr->flags & VAR_ARRAY)) {
	    if (flags & HAX_LEAVE_ERR_MSG) {
		VarErrMsg(interp, part1, part2, (char *) "unset",
		    (char *) needArray);
	    }
	    return -1;
	}
	if (varPtr->searchPtr != NULL) {
	    DeleteSearches(interp, varPtr);
	}
	arrayPtr = varPtr;
	hPtr = Hax_FindHashEntry(varPtr->value.tablePtr, part2);
	if (hPtr == NULL) {
	    if (flags & HAX_LEAVE_ERR_MSG) {
		VarErrMsg(interp, part1, part2, (char *) "unset",
		    (char *) noSuchElement);
	    }
	    return -1;
	}
	varPtr = (Var *) Hax_GetHashValue(hPtr);
    }

    /*
     * If there is a trace active on this variable or if the variable
     * is already being deleted then don't delete the variable:  it
     * isn't safe, since there are procedures higher up on the stack
     * that will use pointers to the variable.  Also don't delete an
     * array if there are traces active on any of its elements.
     */

    if (varPtr->flags &
	    (VAR_TRACE_ACTIVE|VAR_ELEMENT_ACTIVE)) {
	if (flags & HAX_LEAVE_ERR_MSG) {
	    VarErrMsg(interp, part1, part2, (char *) "unset",
		(char *) traceActive);
	}
	return -1;
    }

    /*
     * The code below is tricky, because of the possibility that
     * a trace procedure might try to access a variable being
     * deleted.  To handle this situation gracefully, copy the
     * contents of the variable and its hash table entry to
     * dummy variables, then clean up the actual variable so that
     * it's been completely deleted before the traces are called.
     * Then call the traces, and finally clean up the variable's
     * storage using the dummy copies.
     */

    dummyVar = *varPtr;
    Hax_SetHashValue(&dummyEntry, &dummyVar);
    if (varPtr->upvarUses == 0) {
	Hax_DeleteHashEntry(interp, hPtr);
	ckfree(memoryp, (char *) varPtr);
    } else {
	varPtr->flags = VAR_UNDEFINED;
	varPtr->tracePtr = NULL;
    }

    /*
     * Call trace procedures for the variable being deleted and delete
     * its traces.
     */

    if ((dummyVar.tracePtr != NULL)
	    || ((arrayPtr != NULL) && (arrayPtr->tracePtr != NULL))) {
	(void) CallTraces(iPtr, arrayPtr, &dummyEntry, part1, part2,
		(flags & HAX_GLOBAL_ONLY) | HAX_TRACE_UNSETS);
	while (dummyVar.tracePtr != NULL) {
	    VarTrace *tracePtr = dummyVar.tracePtr;
	    dummyVar.tracePtr = tracePtr->nextPtr;
	    ckfree(memoryp, (char *) tracePtr);
	}
    }

    /*
     * If the variable is an array, delete all of its elements.  This
     * must be done after calling the traces on the array, above (that's
     * the way traces are defined).
     */

    if (dummyVar.flags & VAR_ARRAY) {
	DeleteArray(iPtr, part1, &dummyVar,
	    (flags & HAX_GLOBAL_ONLY) | HAX_TRACE_UNSETS);
    }
    if (dummyVar.flags & VAR_UNDEFINED) {
	if (flags & HAX_LEAVE_ERR_MSG) {
	    VarErrMsg(interp, part1, part2, (char *) "unset",
		(part2 == NULL) ? (char *) noSuchVar : (char *) noSuchElement);
	}
	return -1;
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, %ebp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
testb $0x1, %bpl
jne 0x1148a
movq 0xc8(%r15), %rdi
testq %rdi, %rdi
je 0x1148a
leaq 0x40(%rdi), %rax
jmp 0x11495
leaq 0x68(%r15), %rdi
leaq 0xa8(%r15), %rax
movq 0x280(%r15), %r12
movq %r14, %rsi
callq *(%rax)
testq %rax, %rax
je 0x114e3
movq 0x18(%rax), %r13
testb $0x2, 0x20(%r13)
je 0x114b9
movq 0x28(%r13), %rax
movq 0x18(%rax), %r13
testq %rbx, %rbx
je 0x1150d
testb $0x1, 0x20(%r13)
jne 0x11519
pushq $-0x1
popq %r12
btl $0x9, %ebp
jae 0x116b6
leaq 0x1f54(%rip), %rcx      # 0x1342e
leaq 0x3e9f(%rip), %r8       # 0x15380
jmp 0x114ff
pushq $-0x1
popq %r12
btl $0x9, %ebp
jae 0x116b6
leaq 0x1f36(%rip), %rcx      # 0x1342e
leaq 0x3e70(%rip), %r8       # 0x1536f
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
jmp 0x116b1
movq %rbx, (%rsp)
movq %r13, %rbx
xorl %r13d, %r13d
jmp 0x11542
cmpq $0x0, 0x18(%r13)
je 0x1152b
movq %r15, %rdi
movq %r13, %rsi
callq 0x113bb
movq 0x28(%r13), %rdi
movq %rbx, %rsi
callq *0x40(%rdi)
testq %rax, %rax
je 0x115a7
movq %rbx, (%rsp)
movq 0x18(%rax), %rbx
testb $0x30, 0x20(%rbx)
je 0x11573
pushq $-0x1
popq %r12
btl $0x9, %ebp
jae 0x116b6
leaq 0x1ed1(%rip), %rcx      # 0x1342e
leaq 0x3e5c(%rip), %r8       # 0x153c0
movq %r15, %rdi
movq %r14, %rsi
movq (%rsp), %rdx
jmp 0x116b1
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
leaq 0x10(%rsp), %rcx
movaps %xmm2, 0x20(%rcx)
movaps %xmm1, 0x10(%rcx)
movaps %xmm0, (%rcx)
movq %rcx, 0x58(%rsp)
cmpl $0x0, 0x8(%rbx)
je 0x115c8
movl $0x4, 0x20(%rbx)
andq $0x0, 0x10(%rbx)
jmp 0x115ea
pushq $-0x1
popq %r12
btl $0x9, %ebp
jae 0x116b6
leaq 0x1e72(%rip), %rcx      # 0x1342e
leaq 0x3dd2(%rip), %r8       # 0x15395
jmp 0x114ff
movq %r15, %rdi
movq %rax, %rsi
callq 0x42f0
leaq 0x3bb9(%rip), %rdx      # 0x15193
movq %r12, %rdi
movq %rbx, %rsi
movl $0x2bb, %ecx            # imm = 0x2BB
callq 0x4280
movl %ebp, %eax
andl $0x1, %eax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0x11648
movq 0x8(%rsp), %rax
leal 0x40(%rax), %r9d
leaq 0x40(%rsp), %rdx
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rcx
movq (%rsp), %r8
callq 0x10e7b
leaq 0x3b70(%rip), %rbx      # 0x15193
movq 0x20(%rsp), %rsi
testq %rsi, %rsi
je 0x11654
movq 0x18(%rsi), %rax
movq %rax, 0x20(%rsp)
movq %r12, %rdi
movq %rbx, %rdx
movl $0x2cd, %ecx            # imm = 0x2CD
callq 0x4280
jmp 0x11623
testq %r13, %r13
je 0x11654
cmpq $0x0, 0x10(%r13)
jne 0x115fc
movl 0x30(%rsp), %eax
testb $0x1, %al
je 0x1167a
movq 0x8(%rsp), %rcx
orl $0x40, %ecx
leaq 0x10(%rsp), %rbx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x116c8
movl 0x20(%rbx), %eax
xorl %r12d, %r12d
testb $0x4, %al
je 0x116b6
pushq $-0x1
popq %r12
btl $0x9, %ebp
jae 0x116b6
movq (%rsp), %rdx
testq %rdx, %rdx
leaq 0x3cd6(%rip), %rax      # 0x1536f
leaq 0x3cf5(%rip), %r8       # 0x15395
cmoveq %rax, %r8
leaq 0x1d83(%rip), %rcx      # 0x1342e
movq %r15, %rdi
movq %r14, %rsi
callq 0x10dfb
movl %r12d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq