// File: /workspace/github_cpp_makefile_star2_O3/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
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
incl %r12d
cmpb $0x0, (%r14,%r12)
jne 0x284c4
xorl %eax, %eax
movl %eax, %ecx
incl %eax
cmpb $0x0, (%r15,%rcx)
jne 0x284d0
movslq %eax, %rdi
callq 0x2230
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
incl %ecx
cmpb $0x0, (%r15,%rcx)
jne 0x284e8
movb $0x0, (%rax)
testl %ecx, %ecx
jle 0x2858c
movl %r12d, %edx
xorl %edi, %edi
xorl %r8d, %r8d
testl %r12d, %r12d
jle 0x28562
movb (%r15,%rdi), %r9b
movsbl %r9b, %esi
xorl %r10d, %r10d
cmpb %r9b, (%rbx,%r10)
jne 0x28538
testl %r8d, %r8d
jle 0x28547
movl %r8d, %r13d
movb (%r14,%r10), %r11b
cmpb %r11b, -0x1(%rax,%r13)
jne 0x2854b
movb $0x0, -0x1(%rax,%r13)
decl %r8d
jmp 0x2855a
cmpb %r9b, (%r14,%r10)
jne 0x2855a
movslq %r8d, %r8
movb %r9b, (%rax,%r8)
jmp 0x28557
movb (%r14,%r10), %r11b
cmpb %r9b, %r11b
jne 0x28572
movslq %r8d, %r11
movb %r9b, (%rax,%r11)
incl %r8d
incq %r10
cmpq %r10, %rdx
jne 0x28514
incq %rdi
cmpq %rcx, %rdi
jne 0x28504
testl %r8d, %r8d
sete %bl
jmp 0x2858e
movslq %r8d, %rcx
movsbl -0x1(%rax,%rcx), %edx
leaq 0x643d(%rip), %rdi      # 0x2e9be
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2050
jmp 0x2858e
movb $0x1, %bl
movl %ebx, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq