// File: /workspace/github_c_makefile_star2_O2/inflex[P]ripMIME/boundary-stack.c
// Function: BS_cmp
int BS_cmp( char *boundary, int len )
{

	char testspace[128]; // was 1024
	int testspacelen=127; // was 1023
	int spin=1;
	int nhl=0;
	struct BS_node *node=glb.boundarystack;
	struct BS_node *nodetmp=NULL, *nodedel=NULL;

	if ((!boundary)||(glb.count == 0)) return 0;
	//if ((glb.smallest_length > 0)&&(len < glb.smallest_length)) return 0;
	if (BS_is_long_enough(len) == 0) return 0;

	nhl = BS_non_hyphen_length(boundary);

	DBS LOGGER_log("%s:%d:BS_cmp:DEBUG: possible-boundary='%s', len=%d, smallest=%d, count=%d, NHL=%d"
			, FL
			, boundary
			, len
			, glb.smallest_length
			, glb.count
			, nhl
			);


	// Crop the incoming string to fit in our testspace length
	if (len > testspacelen) len = testspacelen;

	// Copy the potential boundary into our testspace
	snprintf(testspace, testspacelen, "%s", boundary);

	// First, search through the stack looking for a boundary that matches
	// our search criterion
	//
	// When we do find one, we will jump out of this WHILE loop by setting
	// 'spin' to 0.

	while((node)&&(spin))
	{
//		if (node->boundary_length <= len)
		if (node->boundary_nhl == nhl)
		{	
			DBS LOGGER_log("%s:%d:BS_cmp:DEBUG: Comparing '%s' to '%s'", FL, boundary, node->boundary);
			// * 20040903-08H57:PLD: Set boundary length comparison from > 0 to >= 0
			if ((node->boundary != NULL)&&(node->boundary_length >= 0))
			{
				if ((BS_boundary_detect(testspace, node->boundary, node->boundary_length))==0)
				{
					DBS LOGGER_log("%s:%d:BS_cmp:DEBUG: Boundary HIT",FL);
					spin = 0;
				}
			}
		}

		if (spin != 0) node = node->next;
	}

	// If we have a hit on the matching, then, according
	// to nested MIME rules, we must "remove" any previous
	// boundaries
	//
	// We know that we had a HIT in matching if spin == 0, because
	// in our previous code block that's what we set spin to if
	if(spin==0)
	{

		DBS LOGGER_log("%s:%d:BS_cmp:DEBUG: Boundary hit on '%s' == '%s'",FL, boundary,node->boundary);

		// If our "HIT" node is /NOT/ the one on the top of the
		// stack, then we need to pop off and deallocate the nodes
		// PRIOR/Above the hit node.
		//
		// ie, if "NODE" is not the top, then pop off until we
		// do get to the node

		if (node != glb.boundarystack)
		{
			nodetmp = glb.boundarystack;
			while ((nodetmp)&&(nodetmp != node))
			{
				// - Set the node to delete (nodedel) to the current temp
				// node (notetmp)
				// - Increment the nodetmp to the next node in the stack
				// - Free the node to delete (nodedel)

				nodedel = nodetmp;
				nodetmp = nodetmp->next;
				free(nodedel->boundary);
				free(nodedel);
			}
			glb.boundarystack = node;
		}
		return 1;

	}
	else
	{
		return 0;
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
testq %rdi, %rdi
sete %al
movl 0x2157b(%rip), %r15d    # 0x2e560
testl %r15d, %r15d
sete %cl
xorl %r14d, %r14d
orb %al, %cl
jne 0xd17d
movl %esi, %ebp
movl 0x2156d(%rip), %r13d    # 0x2e56c
cmpl $-0x1, %r13d
sete %al
cmpl %esi, %r13d
setg %cl
orb %al, %cl
jne 0xd17d
movq %rdi, %rbx
movq 0x2155a(%rip), %r12     # 0x2e578
callq 0xcc83
movl %eax, %r14d
cmpl $0x0, 0x21523(%rip)     # 0x2e550
je 0xd058
leaq 0xa283(%rip), %rdi      # 0x172b9
leaq 0xa078(%rip), %rsi      # 0x170b5
movl $0x1e2, %edx            # imm = 0x1E2
movq %rbx, %rcx
movl %ebp, %r8d
movl %r13d, %r9d
xorl %eax, %eax
pushq %r14
pushq %r15
callq 0xc2a1
popq %rcx
popq %rdx
leaq 0xa001(%rip), %rdx      # 0x17060
movq %rsp, %rdi
pushq $0x7f
popq %rsi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2140
leaq 0xa03f(%rip), %r13      # 0x170b5
movb $0x1, %r15b
xorl %ebp, %ebp
testb $0x1, %r15b
je 0xd110
testq %r12, %r12
je 0xd110
cmpl %r14d, 0xc(%r12)
jne 0xd106
cmpl $0x0, 0x214b4(%rip)     # 0x2e550
je 0xd0bb
movq (%r12), %r8
leaq 0xa262(%rip), %rdi      # 0x1730b
movq %r13, %rsi
movl $0x1fc, %edx            # imm = 0x1FC
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc2a1
movq (%r12), %rsi
testq %rsi, %rsi
je 0xd106
movl 0x8(%r12), %edx
testl %edx, %edx
js 0xd106
movq %rsp, %rdi
callq 0xce88
testl %eax, %eax
jne 0xd106
movb $0x1, %bpl
xorl %r15d, %r15d
cmpl $0x0, 0x2146a(%rip)     # 0x2e550
je 0xd07b
xorl %r15d, %r15d
leaq 0xa244(%rip), %rdi      # 0x17336
movq %r13, %rsi
movl $0x202, %edx            # imm = 0x202
xorl %eax, %eax
callq 0xc2a1
jmp 0xd07b
movq 0x10(%r12), %r12
jmp 0xd076
xorl %r14d, %r14d
testb $0x1, %bpl
je 0xd17d
cmpl $0x0, 0x21430(%rip)     # 0x2e550
je 0xd143
movq (%r12), %r8
leaq 0xa22a(%rip), %rdi      # 0x17357
leaq 0x9f81(%rip), %rsi      # 0x170b5
movl $0x214, %edx            # imm = 0x214
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc2a1
movq 0x2142e(%rip), %rbx     # 0x2e578
pushq $0x1
popq %r14
cmpq %rbx, %r12
je 0xd17d
testq %rbx, %rbx
je 0xd176
cmpq %r12, %rbx
je 0xd176
movq (%rbx), %rdi
movq 0x10(%rbx), %r15
callq 0x2030
movq %rbx, %rdi
callq 0x2030
movq %r15, %rbx
jmp 0xd153
movq %r12, 0x213fb(%rip)     # 0x2e578
movl %r14d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop