int find_match_closed(unsigned int pos, char *src) {
  pos--;
  for (int weight = 1; weight != 0; pos--) {
    switch (src[pos]) {
    case ']':
      weight++;
      break;
    case '[':
      weight--;
      break;
    }
  }
  return pos;
}


x86 assembly (clang, -O0):
<find_match_closed>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     117c <find_match_closed+0x7c>
mov    -0x10(%rbp),%rax
mov    -0x4(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x18(%rbp)
sub    $0x5b,%eax
je     1160 <find_match_closed+0x60>
jmp    1141 <find_match_closed+0x41>
mov    -0x18(%rbp),%eax
sub    $0x5d,%eax
jne    1169 <find_match_closed+0x69>
jmp    1152 <find_match_closed+0x52>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1169 <find_match_closed+0x69>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    116e <find_match_closed+0x6e>
mov    -0x4(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x4(%rbp)
jmp    111b <find_match_closed+0x1b>
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_match_closed>:
mov    %edi,%eax
add    $0xffffffff,%eax
mov    $0x1,%ecx
jmp    111a <find_match_closed+0x1a>
nopl   0x0(%rax)
add    $0xffffffff,%ecx
add    $0xffffffff,%eax
test   %ecx,%ecx
je     112f <find_match_closed+0x2f>
mov    %eax,%edx
movsbl (%rsi,%rdx,1),%edx
cmp    $0x5b,%edx
je     1110 <find_match_closed+0x10>
cmp    $0x5d,%edx
jne    1113 <find_match_closed+0x13>
add    $0x1,%ecx
jmp    1113 <find_match_closed+0x13>
ret

LLVM IR (-O0):
define i32 @find_match_closed(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%6 = load i32, i32* %3
%7 = add i32 %6, -1
store i32 %7, i32* %3
store i32 1, i32* %5
br label %8
8: ; preds = %25, %2
%9 = load i32, i32* %5
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %28
11: ; preds = %8
%12 = load i8*, i8** %4
%13 = load i32, i32* %3
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
switch i32 %17, label %24 [
i32 93, label %18
i32 91, label %21
]
18: ; preds = %11
%19 = load i32, i32* %5
%20 = add nsw i32 %19, 1
store i32 %20, i32* %5
br label %24
21: ; preds = %11
%22 = load i32, i32* %5
%23 = add nsw i32 %22, -1
store i32 %23, i32* %5
br label %24
24: ; preds = %11, %21, %18
br label %25
25: ; preds = %24
%26 = load i32, i32* %3
%27 = add i32 %26, -1
store i32 %27, i32* %3
br label %8
28: ; preds = %8
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
define i32 @find_match_closed(i32 %0, i8* nocapture %1) {
%3 = add i32 %0, -1
br label %5
4: ; preds = %16
ret i32 %18
5: ; preds = %2, %16
%6 = phi i32 [ %3, %2 ], [ %18, %16 ]
%7 = phi i32 [ 1, %2 ], [ %17, %16 ]
%8 = zext i32 %6 to i64
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
switch i32 %11, label %16 [
i32 93, label %12
i32 91, label %14
]
12: ; preds = %5
%13 = add nsw i32 %7, 1
br label %16
14: ; preds = %5
%15 = add nsw i32 %7, -1
br label %16
16: ; preds = %12, %14, %5
%17 = phi i32 [ %7, %5 ], [ %15, %14 ], [ %13, %12 ]
%18 = add i32 %6, -1
%19 = icmp eq i32 %17, 0
br i1 %19, label %4, label %5
}