// File: /workspace/github_c_makefile_star2_O1/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 %dl
movl 0x21daa(%rip), %ebp     # 0x2f560
testl %ebp, %ebp
sete %cl
xorl %eax, %eax
orb %dl, %cl
jne 0xd95c
movl %esi, %r15d
movl 0x21d9d(%rip), %r12d    # 0x2f56c
cmpl $-0x1, %r12d
sete %dl
cmpl %esi, %r12d
setg %cl
orb %dl, %cl
jne 0xd95c
movq %rdi, %rbx
movq 0x21d8a(%rip), %r13     # 0x2f578
callq 0xd430
movl %eax, %r14d
cmpl $0x0, 0x21d53(%rip)     # 0x2f550
je 0xd829
leaq 0xaac3(%rip), %rdi      # 0x182c9
leaq 0xa8b8(%rip), %rsi      # 0x180c5
movl $0x1e2, %edx            # imm = 0x1E2
movq %rbx, %rcx
movl %r15d, %r8d
movl %r12d, %r9d
xorl %eax, %eax
pushq %r14
pushq %rbp
callq 0xc960
addq $0x10, %rsp
leaq 0xa85d(%rip), %rdx      # 0x1808d
movq %rsp, %rdi
movl $0x7f, %esi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2140
testq %r13, %r13
je 0xd8e7
movq %rsp, %rbp
movb $0x1, %r12b
cmpl %r14d, 0xc(%r13)
jne 0xd89e
cmpl $0x0, 0x21cf2(%rip)     # 0x2f550
je 0xd881
movq (%r13), %r8
leaq 0xaab0(%rip), %rdi      # 0x1831b
leaq 0xa853(%rip), %rsi      # 0x180c5
movl $0x1fc, %edx            # imm = 0x1FC
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc960
movq (%r13), %rsi
testq %rsi, %rsi
je 0xd89e
movl 0x8(%r13), %edx
testl %edx, %edx
js 0xd89e
movq %rbp, %rdi
callq 0xd644
testl %eax, %eax
je 0xd8b7
xorl %r15d, %r15d
testb %r15b, %r15b
jne 0xd8aa
movq 0x10(%r13), %r13
testq %r13, %r13
setne %al
testb %al, %r12b
jne 0xd84e
jmp 0xd8ea
movb $0x1, %r15b
cmpl $0x0, 0x21c8f(%rip)     # 0x2f550
je 0xd8e2
xorl %r12d, %r12d
leaq 0xaa79(%rip), %rdi      # 0x18346
leaq 0xa7f1(%rip), %rsi      # 0x180c5
movl $0x202, %edx            # imm = 0x202
xorl %eax, %eax
callq 0xc960
jmp 0xd8a1
xorl %r12d, %r12d
jmp 0xd8a1
xorl %r15d, %r15d
testb %r15b, %r15b
movl $0x0, %eax
je 0xd95c
cmpl $0x0, 0x21c55(%rip)     # 0x2f550
je 0xd91e
movq (%r13), %r8
leaq 0xaa5f(%rip), %rdi      # 0x18367
leaq 0xa7b6(%rip), %rsi      # 0x180c5
movl $0x214, %edx            # imm = 0x214
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc960
movq 0x21c53(%rip), %r14     # 0x2f578
cmpq %r14, %r13
je 0xd957
testq %r14, %r14
je 0xd950
movq %r14, %rbx
movq (%r14), %rdi
movq 0x10(%r14), %r14
callq 0x2030
movq %rbx, %rdi
callq 0x2030
testq %r14, %r14
je 0xd950
cmpq %r13, %r14
jne 0xd92f
movq %r13, 0x21c21(%rip)     # 0x2f578
movl $0x1, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop