// File: /workspace/github_cpp_makefile_star2_O2/gersteinlab[P]MUSIC/src/ms_ansi_string.cpp
// Function: t_string::is_balanced(char*, char*, char*)
bool t_string::is_balanced(char* str, char* left_pars, char* right_pars)
{
        int n_pars = t_string::string_length(left_pars);

        // This is the list of left parenthesis characters in the string.
        char* left_par_chars = (char*)malloc(sizeof(char) * (t_string::string_length(str) + 1));

        // Count the left and right parenthesis characters, they should be equaal in number, watch the structure along the way.
        int l_str = t_string::string_length(str);
        int l_left_par_chars = 0;
        left_par_chars[l_left_par_chars] = 0; // Initialize.
        for(int i = 0; i < l_str; i++)
        {
                // Check all the parenthesis characters to check if this is a parenthesis character.
                for(int i_par = 0; i_par < n_pars; i_par++)
                {
                        // Check first for pairs that close a frame.
                        if(right_pars[i_par] == str[i])
                        {
                                // Make sure that this does close the last left parenthesis character.
                                if((l_left_par_chars < 1) ||
                                        (l_left_par_chars >= 1 && left_par_chars[l_left_par_chars-1] != left_pars[i_par]))
                                {
                                        // This character may actually be a left parenthesis, whose left and right characters are the same.
                                        if(left_pars[i_par] == right_pars[i_par])
                                        {
                                                // Interpret this as the left parenthesis characters.
                                                left_par_chars[l_left_par_chars] = str[i];
                                                l_left_par_chars++;
                                        }
                                        else
                                        {
                                                printf("Knotted parenthesis: Right parenthesis character %c is closing left parenthesis character %c.\n", right_pars[i_par], left_par_chars[l_left_par_chars-1]);
                                                return(false);
                                        }
                                }
                                else
                                {
                                        // Note that for the pairs of parenthesis characters whose left and right elements are the same, nesting is not possible.

                                        // Remove this left parenthesis character since its matching right parenthesis is found.
                                        left_par_chars[l_left_par_chars-1] = 0;
                                        l_left_par_chars--;
                                }
                        }
                        else if(left_pars[i_par] == str[i])
                        {
                                // Add this left parenthesis character to the end of list of left parenthesis characters encountered so far.
                                left_par_chars[l_left_par_chars] = str[i];
                                l_left_par_chars++;
                        }
                } // i_par loop.

                // Update par_cnt if this is a parenthesis character, depending on it being left or right parenthesis character.

                // If the last parenthesis character was a left one, the new character cannot be a right one and vice versa. (No knotted pairs.)
        } // i loop

        if(l_left_par_chars == 0)
        {
                return(true);
        }
        else
        {
                return(false);
        }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq %rsi, %rdi
callq 0x20d90
movl %eax, %r12d
movq %r15, %rdi
callq 0x20d90
movl %eax, %r13d
leal 0x1(%r13), %eax
movslq %eax, %rdi
callq 0x2220
movb $0x0, (%rax)
xorl %ecx, %ecx
testl %r12d, %r12d
cmovlel %ecx, %r12d
testl %r13d, %r13d
cmovlel %ecx, %r13d
xorl %edx, %edx
cmpq %r13, %rcx
je 0x221eb
xorl %esi, %esi
cmpq %rsi, %r12
je 0x221c1
movb (%rbx,%rsi), %r8b
movb (%r15,%rcx), %r9b
movslq %edx, %rdi
cmpb %r9b, %r8b
jne 0x2219d
testl %edx, %edx
jle 0x221ad
movl %edx, %r10d
movb (%r14,%rsi), %r9b
cmpb %r9b, -0x1(%rax,%r10)
jne 0x221b1
movb $0x0, -0x1(%rax,%r10)
decl %edx
jmp 0x221bc
cmpb %r9b, (%r14,%rsi)
jne 0x221bc
movb %r9b, (%rax,%rdi)
incl %edi
movl %edi, %edx
jmp 0x221bc
movb (%r14,%rsi), %r9b
cmpb %r8b, %r9b
jne 0x221c6
movb %r8b, (%rax,%rdi)
incl %edx
incq %rsi
jmp 0x2216c
incq %rcx
jmp 0x22161
movsbl %r8b, %esi
movsbl -0x1(%rax,%rdi), %edx
leaq 0x681f(%rip), %rdi      # 0x289f5
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2050
movl %ebx, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
testl %edx, %edx
sete %bl
jmp 0x221df