#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ assert(int);
scalar_t__ mpl_is_valid_matrix_symbol(char);
char *mpl_skip_closure(char *, char, char);
unsigned long mpl_get_valid_matrix_length(const char *rawmatrix) {
  unsigned long len = 0;
  char *matptr = (char *)rawmatrix;
  do {
    if (mpl_is_valid_matrix_symbol(*matptr)) {
      ++len;
    } else if (*matptr == '[') {
      matptr = mpl_skip_closure(matptr, '[', ']');
      assert(!(matptr < 0));
    }
    ++matptr;
  } while (*matptr);
  return len;
}


x86 assembly (clang, -O0):
<mpl_get_valid_matrix_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1040 <mpl_is_valid_matrix_symbol@plt>
cmp    $0x0,%rax
je     1173 <mpl_get_valid_matrix_length+0x43>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11b3 <mpl_get_valid_matrix_length+0x83>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5b,%eax
jne    11ae <mpl_get_valid_matrix_length+0x7e>
mov    -0x18(%rbp),%rdi
mov    $0x5b,%esi
mov    $0x5d,%edx
call   1030 <mpl_skip_closure@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setb   %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
jmp    11b3 <mpl_get_valid_matrix_length+0x83>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    114c <mpl_get_valid_matrix_length+0x1c>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpl_get_valid_matrix_length>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    (%rdi),%al
xor    %r14d,%r14d
jmp    1150 <mpl_get_valid_matrix_length+0x20>
xchg   %ax,%ax
add    $0x1,%r14
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     1183 <mpl_get_valid_matrix_length+0x53>
movsbl %al,%edi
call   1040 <mpl_is_valid_matrix_symbol@plt>
test   %rax,%rax
jne    1140 <mpl_get_valid_matrix_length+0x10>
cmpb   $0x5b,(%rbx)
jne    1144 <mpl_get_valid_matrix_length+0x14>
mov    %rbx,%rdi
mov    $0x5b,%esi
mov    $0x5d,%edx
call   1030 <mpl_skip_closure@plt>
mov    %rax,%rbx
mov    $0x1,%edi
call   1050 <assert@plt>
jmp    1144 <mpl_get_valid_matrix_length+0x14>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @mpl_get_valid_matrix_length(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i8* %0, i8** %2
store i64 0, i64* %3
%5 = load i8*, i8** %2
store i8* %5, i8** %4
br label %6
6: ; preds = %31, %1
%7 = load i8*, i8** %4
%8 = load i8, i8* %7
%9 = call i64 @mpl_is_valid_matrix_symbol(i8 signext %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i64, i64* %3
%13 = add i64 %12, 1
store i64 %13, i64* %3
br label %28
14: ; preds = %6
%15 = load i8*, i8** %4
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 91
br i1 %18, label %19, label %27
19: ; preds = %14
%20 = load i8*, i8** %4
%21 = call i8* @mpl_skip_closure(i8* %20, i8 signext 91, i8 signext 93)
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = icmp ult i8* %22, null
%24 = xor i1 %23, true
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
br label %27
27: ; preds = %19, %14
br label %28
28: ; preds = %27, %11
%29 = load i8*, i8** %4
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %4
br label %31
31: ; preds = %28
%32 = load i8*, i8** %4
%33 = load i8, i8* %32
%34 = icmp ne i8 %33, 0
br i1 %34, label %6, label %35
35: ; preds = %31
%36 = load i64, i64* %3
ret i64 %36
}

LLVM IR (-O2):
define i64 @mpl_get_valid_matrix_length(i8* %0) {
%2 = load i8, i8* %0
br label %3
3: ; preds = %17, %1
%4 = phi i8 [ %2, %1 ], [ %21, %17 ]
%5 = phi i64 [ 0, %1 ], [ %18, %17 ]
%6 = phi i8* [ %0, %1 ], [ %20, %17 ]
%7 = tail call i64 @mpl_is_valid_matrix_symbol(i8 signext %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = add i64 %5, 1
br label %17
11: ; preds = %3
%12 = load i8, i8* %6
%13 = icmp eq i8 %12, 91
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = tail call i8* @mpl_skip_closure(i8* %6, i8 signext 91, i8 signext 93)
%16 = tail call i32 @assert(i32 1)
br label %17
17: ; preds = %11, %14, %9
%18 = phi i64 [ %10, %9 ], [ %5, %14 ], [ %5, %11 ]
%19 = phi i8* [ %6, %9 ], [ %15, %14 ], [ %6, %11 ]
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %3
23: ; preds = %17
ret i64 %18
}