// File: /workspace/github_cpp_makefile_star2_O1/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
incl %r12d
cmpb $0x0, (%r14,%r12)
jne 0x28bd2
xorl %eax, %eax
movl %eax, %ecx
incl %eax
cmpb $0x0, (%r15,%rcx)
jne 0x28bde
movslq %eax, %rdi
callq 0x2230
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
incl %ecx
cmpb $0x0, (%r15,%rcx)
jne 0x28bf6
movb $0x0, (%rax)
testl %ecx, %ecx
setle %bpl
jle 0x28c99
movl %ecx, %ecx
movl %r12d, %edx
xorl %edi, %edi
xorl %r13d, %r13d
testl %r12d, %r12d
jle 0x28c73
movb (%r15,%rdi), %r8b
xorl %r9d, %r9d
movsbl (%rbx,%r9), %esi
cmpb %r8b, %sil
jne 0x28c4c
testl %r13d, %r13d
jle 0x28c5b
movl %r13d, %r10d
movb -0x1(%rax,%r10), %r11b
cmpb (%r14,%r9), %r11b
jne 0x28c5b
movb $0x0, -0x1(%rax,%r10)
decl %r13d
jmp 0x28c6b
cmpb %r8b, (%r14,%r9)
jne 0x28c6b
movslq %r13d, %r13
movb %r8b, (%rax,%r13)
jmp 0x28c68
cmpb %sil, (%r14,%r9)
jne 0x28c81
movslq %r13d, %rsi
movb %r8b, (%rax,%rsi)
incl %r13d
incq %r9
cmpq %r9, %rdx
jne 0x28c24
incq %rdi
cmpq %rcx, %rdi
setae %bpl
jne 0x28c18
jmp 0x28c9c
movslq %r13d, %rcx
movsbl -0x1(%rax,%rcx), %edx
leaq 0x6d2e(%rip), %rdi      # 0x2f9be
xorl %eax, %eax
callq 0x2050
jmp 0x28c9c
xorl %r13d, %r13d
testl %r13d, %r13d
sete %al
andb %bpl, %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq