// File: /workspace/github_c_makefile_star2_O3/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 $0x98, %rsp
movq %rdi, 0x8(%rsp)
testq %rdi, %rdi
sete %al
movl 0x21f35(%rip), %ebx     # 0x2f560
testl %ebx, %ebx
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0xd72d
movl %esi, %r15d
movl 0x21f28(%rip), %r12d    # 0x2f56c
cmpl $-0x1, %r12d
sete %al
cmpl %esi, %r12d
setg %cl
orb %al, %cl
jne 0xd72d
movq 0x21f18(%rip), %r13     # 0x2f578
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
callq 0xd2a1
movl %eax, %r14d
cmpl $0x0, 0x21ed9(%rip)     # 0x2f550
je 0xd6a3
leaq 0xac39(%rip), %rdi      # 0x182b9
leaq 0xaa2e(%rip), %rsi      # 0x180b5
movl $0x1e2, %edx            # imm = 0x1E2
movq %rbp, %rcx
movl %r15d, %r8d
movl %r12d, %r9d
xorl %eax, %eax
pushq %r14
pushq %rbx
callq 0xc84f
addq $0x10, %rsp
leaq 0xa9b6(%rip), %rdx      # 0x18060
movq %rbp, %rcx
xorl %ebp, %ebp
leaq 0x10(%rsp), %rdi
movl $0x7f, %esi
xorl %eax, %eax
callq 0x2140
testq %r13, %r13
je 0xd72d
leaq 0x10(%rsp), %r15
leaq 0xac3a(%rip), %r12      # 0x1830b
leaq 0xa9dd(%rip), %rbp      # 0x180b5
movq 0x8(%rsp), %rbx
cmpl %r14d, 0xc(%r13)
jne 0xd722
cmpl $0x0, 0x21e66(%rip)     # 0x2f550
je 0xd705
movq (%r13), %r8
movq %r12, %rdi
movq %rbp, %rsi
movl $0x1fc, %edx            # imm = 0x1FC
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc84f
movq (%r13), %rsi
testq %rsi, %rsi
je 0xd722
movl 0x8(%r13), %edx
testl %edx, %edx
js 0xd722
movq %r15, %rdi
callq 0xd4b4
testl %eax, %eax
je 0xd741
movq 0x10(%r13), %r13
testq %r13, %r13
jne 0xd6dd
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x21e08(%rip)     # 0x2f550
je 0xd78e
leaq 0xabe5(%rip), %rdi      # 0x18336
leaq 0xa95d(%rip), %rsi      # 0x180b5
movl $0x202, %edx            # imm = 0x202
xorl %eax, %eax
callq 0xc84f
cmpl $0x0, 0x21de5(%rip)     # 0x2f550
je 0xd78e
movq (%r13), %r8
leaq 0xabdf(%rip), %rdi      # 0x18357
leaq 0xa936(%rip), %rsi      # 0x180b5
movl $0x214, %edx            # imm = 0x214
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc84f
movq 0x21de3(%rip), %r14     # 0x2f578
movl $0x1, %ebp
cmpq %r14, %r13
je 0xd72d
testq %r14, %r14
je 0xd7c5
movq %r14, %rbx
movq (%r14), %rdi
movq 0x10(%r14), %r14
callq 0x2030
movq %rbx, %rdi
callq 0x2030
testq %r14, %r14
je 0xd7c5
cmpq %r13, %r14
jne 0xd7a4
movq %r13, 0x21dac(%rip)     # 0x2f578
jmp 0xd72d
nopl (%rax)