#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*/ dbgprintf(char *, char const *);
char *find_matching_parenthesis(const char *s) {
  int lev = 0;
  dbgprintf("find_matching_parenthesis: %s\n", s);
  if (*s == '(')
    ++s;
  while (*s != '\0') {
    if (*s == '(')
      ++lev;
    else if (*s == ')') {
      if (lev == 0)
        return (char *)s + 1;
      else {
        --lev;
      }
    }
    ++s;
  }
  return (char *)s;
}


x86 assembly (clang, -O0):
<find_matching_parenthesis>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xed2(%rip),%rdi
call   1030 <dbgprintf@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x28,%eax
jne    114f <find_matching_parenthesis+0x3f>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1154 <find_matching_parenthesis+0x44>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11d1 <find_matching_parenthesis+0xc1>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x28,%eax
jne    1182 <find_matching_parenthesis+0x72>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c0 <find_matching_parenthesis+0xb0>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x29,%eax
jne    11bb <find_matching_parenthesis+0xab>
cmpl   $0x0,-0x14(%rbp)
jne    11ad <find_matching_parenthesis+0x9d>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11d9 <find_matching_parenthesis+0xc9>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11bb <find_matching_parenthesis+0xab>
jmp    11c0 <find_matching_parenthesis+0xb0>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1154 <find_matching_parenthesis+0x44>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_matching_parenthesis>:
push   %rbx
mov    %rdi,%rbx
lea    0xee5(%rip),%rdi
mov    %rbx,%rsi
call   1030 <dbgprintf@plt>
xor    %eax,%eax
cmpb   $0x28,(%rbx)
sete   %al
add    %rbx,%rax
xor    %ecx,%ecx
jmp    1147 <find_matching_parenthesis+0x37>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%ecx
add    $0x1,%rax
movzbl (%rax),%edx
cmp    $0x28,%dl
je     1140 <find_matching_parenthesis+0x30>
cmp    $0x29,%dl
je     1160 <find_matching_parenthesis+0x50>
test   %dl,%dl
jne    1167 <find_matching_parenthesis+0x57>
jmp    1171 <find_matching_parenthesis+0x61>
nopw   0x0(%rax,%rax,1)
test   %ecx,%ecx
je     116d <find_matching_parenthesis+0x5d>
add    $0xffffffff,%ecx
add    $0x1,%rax
jmp    1147 <find_matching_parenthesis+0x37>
add    $0x1,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [31 x i8] c"find_matching_parenthesis: %s\0A\00"
define i8* @find_matching_parenthesis(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
%5 = load i8*, i8** %3
%6 = call i32 @dbgprintf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = load i8*, i8** %3
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp eq i32 %9, 40
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i32 1
store i8* %13, i8** %3
br label %14
14: ; preds = %11, %1
br label %15
15: ; preds = %44, %14
%16 = load i8*, i8** %3
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %47
20: ; preds = %15
%21 = load i8*, i8** %3
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 40
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i32, i32* %4
%27 = add nsw i32 %26, 1
store i32 %27, i32* %4
br label %44
28: ; preds = %20
%29 = load i8*, i8** %3
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 41
br i1 %32, label %33, label %43
33: ; preds = %28
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i8*, i8** %3
%38 = getelementptr inbounds i8, i8* %37, i64 1
store i8* %38, i8** %2
br label %49
39: ; preds = %33
%40 = load i32, i32* %4
%41 = add nsw i32 %40, -1
store i32 %41, i32* %4
br label %42
42: ; preds = %39
br label %43
43: ; preds = %42, %28
br label %44
44: ; preds = %43, %25
%45 = load i8*, i8** %3
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %3
br label %15
47: ; preds = %15
%48 = load i8*, i8** %3
store i8* %48, i8** %2
br label %49
49: ; preds = %47, %36
%50 = load i8*, i8** %2
ret i8* %50
}

LLVM IR (-O2):
@.str = [31 x i8] c"find_matching_parenthesis: %s\0A\00"
define i8* @find_matching_parenthesis(i8* %0) {
%2 = tail call i32 @dbgprintf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 40
%5 = zext i1 %4 to i64
%6 = getelementptr i8, i8* %0, i64 %5
br label %7
7: ; preds = %19, %1
%8 = phi i8* [ %6, %1 ], [ %21, %19 ]
%9 = phi i32 [ 0, %1 ], [ %20, %19 ]
%10 = load i8, i8* %8
switch i8 %10, label %19 [
i8 0, label %22
i8 40, label %11
i8 41, label %13
]
11: ; preds = %7
%12 = add nsw i32 %9, 1
br label %19
13: ; preds = %7
%14 = icmp eq i32 %9, 0
br i1 %14, label %15, label %17
15: ; preds = %13
%16 = getelementptr inbounds i8, i8* %8, i64 1
br label %22
17: ; preds = %13
%18 = add nsw i32 %9, -1
br label %19
19: ; preds = %7, %17, %11
%20 = phi i32 [ %12, %11 ], [ %18, %17 ], [ %9, %7 ]
%21 = getelementptr inbounds i8, i8* %8, i64 1
br label %7
22: ; preds = %7, %15
%23 = phi i8* [ %16, %15 ], [ %8, %7 ]
ret i8* %23
}