// File: 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:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
movq 0x30(%rsp), %rdi
callq 0x47640
movl %eax, 0x24(%rsp)
movq 0x38(%rsp), %rdi
callq 0x47640
addl $0x1, %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x2230
movq %rax, 0x18(%rsp)
movq 0x38(%rsp), %rdi
callq 0x47640
movl %eax, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
movq 0x18(%rsp), %rax
movslq 0x10(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x14(%rsp), %eax
jge 0x49d8b
movl $0x0, 0x8(%rsp)
movl 0x8(%rsp), %eax
cmpl 0x24(%rsp), %eax
jge 0x49d79
movq 0x28(%rsp), %rax
movslq 0x8(%rsp), %rcx
movsbl (%rax,%rcx), %eax
movq 0x38(%rsp), %rcx
movslq 0xc(%rsp), %rdx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x49d20
cmpl $0x1, 0x10(%rsp)
jl 0x49c7e
cmpl $0x1, 0x10(%rsp)
jl 0x49d00
movq 0x18(%rsp), %rax
movl 0x10(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
movq 0x30(%rsp), %rcx
movslq 0x8(%rsp), %rdx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
je 0x49d00
movq 0x30(%rsp), %rax
movslq 0x8(%rsp), %rcx
movsbl (%rax,%rcx), %eax
movq 0x28(%rsp), %rcx
movslq 0x8(%rsp), %rdx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x49cc5
movq 0x38(%rsp), %rax
movslq 0xc(%rsp), %rcx
movb (%rax,%rcx), %dl
movq 0x18(%rsp), %rax
movslq 0x10(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x49cfe
movq 0x28(%rsp), %rax
movslq 0x8(%rsp), %rcx
movsbl (%rax,%rcx), %esi
movq 0x18(%rsp), %rax
movl 0x10(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %edx
leaq 0xd0df(%rip), %rdi      # 0x56dcc
movb $0x0, %al
callq 0x2050
movb $0x0, 0x47(%rsp)
jmp 0x49d9e
jmp 0x49d1e
movq 0x18(%rsp), %rax
movl 0x10(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movl 0x10(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x49d67
movq 0x30(%rsp), %rax
movslq 0x8(%rsp), %rcx
movsbl (%rax,%rcx), %eax
movq 0x38(%rsp), %rcx
movslq 0xc(%rsp), %rdx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x49d65
movq 0x38(%rsp), %rax
movslq 0xc(%rsp), %rcx
movb (%rax,%rcx), %dl
movq 0x18(%rsp), %rax
movslq 0x10(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x49d67
jmp 0x49d69
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0x49c11
jmp 0x49d7b
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x49bfb
cmpl $0x0, 0x10(%rsp)
jne 0x49d99
movb $0x1, 0x47(%rsp)
jmp 0x49d9e
movb $0x0, 0x47(%rsp)
movb 0x47(%rsp), %al
andb $0x1, %al
addq $0x48, %rsp
retq
nopl (%rax)