#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 */
bool is_balanced(const char *str) {
  if (*str++ != '(') {
    return true;
  }
  int unbalanced_parens = 1;
  while (unbalanced_parens > 0) {
    if (*str == '\0')
      return false;
    if (*str == ')')
      unbalanced_parens--;
    str++;
  }
  return true;
}


x86 assembly (clang, -O0):
<is_balanced>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%eax
cmp    $0x28,%eax
je     112f <is_balanced+0x2f>
movl   $0x1,-0x4(%rbp)
jmp    118d <is_balanced+0x8d>
movl   $0x1,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1186 <is_balanced+0x86>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    115c <is_balanced+0x5c>
movl   $0x0,-0x4(%rbp)
jmp    118d <is_balanced+0x8d>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x29,%eax
jne    1175 <is_balanced+0x75>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1136 <is_balanced+0x36>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_balanced>:
mov    $0x1,%eax
cmpb   $0x28,(%rdi)
jne    1122 <is_balanced+0x22>
add    $0x1,%rdi
xchg   %ax,%ax
movzbl (%rdi),%ecx
cmp    $0x29,%cl
je     1122 <is_balanced+0x22>
add    $0x1,%rdi
test   %cl,%cl
jne    1110 <is_balanced+0x10>
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @is_balanced(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = getelementptr inbounds i8, i8* %5, i32 1
store i8* %6, i8** %3
%7 = load i8, i8* %5
%8 = sext i8 %7 to i32
%9 = icmp ne i32 %8, 40
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 1, i32* %2
br label %33
11: ; preds = %1
store i32 1, i32* %4
br label %12
12: ; preds = %29, %11
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %32
15: ; preds = %12
%16 = load i8*, i8** %3
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 0, i32* %2
br label %33
21: ; preds = %15
%22 = load i8*, i8** %3
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 41
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* %4
%28 = add nsw i32 %27, -1
store i32 %28, i32* %4
br label %29
29: ; preds = %26, %21
%30 = load i8*, i8** %3
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %3
br label %12
32: ; preds = %12
store i32 1, i32* %2
br label %33
33: ; preds = %32, %20, %10
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
define i32 @is_balanced(i8* nocapture %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 40
br i1 %3, label %4, label %9
4: ; preds = %1, %4
%5 = phi i8* [ %6, %4 ], [ %0, %1 ]
%6 = getelementptr inbounds i8, i8* %5, i64 1
%7 = load i8, i8* %6
switch i8 %7, label %4 [
i8 0, label %8
i8 41, label %9
]
8: ; preds = %4
br label %9
9: ; preds = %4, %8, %1
%10 = phi i32 [ 1, %1 ], [ 0, %8 ], [ 1, %4 ]
ret i32 %10
}