#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 */
char *strchr(char *, char);
char *findRbrace(char *s) {
  char *p = s, *pl, *pr;
  int nl = 0, nr = 0;
  while (nr <= nl) {
    pl = strchr(p, '{');
    pr = strchr(p, '}');
    if (!pr)
      return NULL;
    if (!pl || pr < pl) {
      p = pr + 1;
      nr++;
    } else {
      p = pl + 1;
      nl++;
    }
  }
  return pr;
}


x86 assembly (clang, -O0):
<findRbrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jg     11c7 <findRbrace+0xb7>
mov    -0x18(%rbp),%rdi
mov    $0x7b,%esi
call   1030 <strchr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x7d,%esi
call   1030 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    117a <findRbrace+0x6a>
movq   $0x0,-0x8(%rbp)
jmp    11cf <findRbrace+0xbf>
cmpq   $0x0,-0x20(%rbp)
je     1193 <findRbrace+0x83>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11ad <findRbrace+0x9d>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11c2 <findRbrace+0xb2>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1132 <findRbrace+0x22>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findRbrace>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %r15d,%r15d
xor    %ebp,%ebp
nop
mov    %r14,%rdi
mov    $0x7b,%esi
call   1030 <strchr@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x7d,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     116e <findRbrace+0x5e>
test   %rbx,%rbx
sete   %cl
cmp    %rbx,%rax
setb   %dl
or     %cl,%dl
cmovne %rax,%rbx
movzbl %dl,%ecx
xor    $0x1,%dl
movzbl %dl,%edx
add    %edx,%ebp
add    %ecx,%r15d
add    $0x1,%rbx
mov    %rbx,%r14
cmp    %ebp,%r15d
jbe    1120 <findRbrace+0x10>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @findRbrace(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
store i8* %9, i8** %4
store i32 0, i32* %7
store i32 0, i32* %8
br label %10
10: ; preds = %39, %1
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = icmp sle i32 %11, %12
br i1 %13, label %14, label %40
14: ; preds = %10
%15 = load i8*, i8** %4
%16 = call i8* @strchr(i8* %15, i8 signext 123)
store i8* %16, i8** %5
%17 = load i8*, i8** %4
%18 = call i8* @strchr(i8* %17, i8 signext 125)
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %14
store i8* null, i8** %2
br label %42
22: ; preds = %14
%23 = load i8*, i8** %5
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i8*, i8** %6
%27 = load i8*, i8** %5
%28 = icmp ult i8* %26, %27
br i1 %28, label %29, label %34
29: ; preds = %25, %22
%30 = load i8*, i8** %6
%31 = getelementptr inbounds i8, i8* %30, i64 1
store i8* %31, i8** %4
%32 = load i32, i32* %8
%33 = add nsw i32 %32, 1
store i32 %33, i32* %8
br label %39
34: ; preds = %25
%35 = load i8*, i8** %5
%36 = getelementptr inbounds i8, i8* %35, i64 1
store i8* %36, i8** %4
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
br label %39
39: ; preds = %34, %29
br label %10
40: ; preds = %10
%41 = load i8*, i8** %6
store i8* %41, i8** %2
br label %42
42: ; preds = %40, %21
%43 = load i8*, i8** %2
ret i8* %43
}

LLVM IR (-O2):
define i8* @findRbrace(i8* %0) {
br label %2
2: ; preds = %1, %9
%3 = phi i32 [ 0, %1 ], [ %18, %9 ]
%4 = phi i32 [ 0, %1 ], [ %16, %9 ]
%5 = phi i8* [ %0, %1 ], [ %19, %9 ]
%6 = tail call i8* @strchr(i8* dereferenceable(1) %5, i8 signext 123)
%7 = tail call i8* @strchr(i8* dereferenceable(1) %5, i8 signext 125)
%8 = icmp eq i8* %7, null
br i1 %8, label %21, label %9
9: ; preds = %2
%10 = icmp eq i8* %6, null
%11 = icmp ult i8* %7, %6
%12 = select i1 %10, i1 true, i1 %11
%13 = select i1 %12, i8* %7, i8* %6
%14 = xor i1 %12, true
%15 = zext i1 %14 to i32
%16 = add nuw nsw i32 %4, %15
%17 = zext i1 %12 to i32
%18 = add nuw nsw i32 %3, %17
%19 = getelementptr inbounds i8, i8* %13, i64 1
%20 = icmp ugt i32 %18, %16
br i1 %20, label %21, label %2
21: ; preds = %9, %2
ret i8* %7
}