const char *find_corresponding_lparen(p) const char *p;
{
  const char *q;
  int paren_depth;
  for (paren_depth = 1, q = p - 1; paren_depth; q--) {
    switch (*q) {
    case ')':
      paren_depth++;
      break;
    case '(':
      paren_depth--;
      break;
    }
  }
  return ++q;
}


x86 assembly (clang, -O0):
<find_corresponding_lparen>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     117b <find_corresponding_lparen+0x7b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x18(%rbp)
sub    $0x28,%eax
je     115c <find_corresponding_lparen+0x5c>
jmp    113d <find_corresponding_lparen+0x3d>
mov    -0x18(%rbp),%eax
sub    $0x29,%eax
jne    1165 <find_corresponding_lparen+0x65>
jmp    114e <find_corresponding_lparen+0x4e>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1165 <find_corresponding_lparen+0x65>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    116a <find_corresponding_lparen+0x6a>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    111b <find_corresponding_lparen+0x1b>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_corresponding_lparen>:
mov    %rdi,%rax
mov    $0x1,%ecx
jmp    111b <find_corresponding_lparen+0x1b>
nopw   0x0(%rax,%rax,1)
add    $0xffffffff,%ecx
add    $0xffffffffffffffff,%rax
test   %ecx,%ecx
je     112e <find_corresponding_lparen+0x2e>
movsbl -0x1(%rax),%edx
cmp    $0x28,%edx
je     1110 <find_corresponding_lparen+0x10>
cmp    $0x29,%edx
jne    1113 <find_corresponding_lparen+0x13>
add    $0x1,%ecx
jmp    1113 <find_corresponding_lparen+0x13>
ret

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

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