#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*/ assert(int);
unsigned max_nest_depth(const char *str) {
  unsigned res = 0;
  unsigned opened = 0;
  size_t i;
  for (i = 0; str[i] != '\0'; i++) {
    if (str[i] == '(') {
      opened++;
      res = ((res) > (opened) ? (res) : (opened));
    } else if (str[i] == ')') {
      assert(opened > 0);
      opened--;
    }
  }
  assert(opened == 0);
  return res;
}


x86 assembly (clang, -O0):
<max_nest_depth>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11d7 <max_nest_depth+0xc7>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x28,%eax
jne    118d <max_nest_depth+0x7d>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jbe    117c <max_nest_depth+0x6c>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1182 <max_nest_depth+0x72>
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11c1 <max_nest_depth+0xb1>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x29,%eax
jne    11bc <max_nest_depth+0xac>
cmpl   $0x0,-0x10(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    11c1 <max_nest_depth+0xb1>
jmp    11c6 <max_nest_depth+0xb6>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1132 <max_nest_depth+0x22>
cmpl   $0x0,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<max_nest_depth>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
xor    %r14d,%r14d
xor    %ebp,%ebp
jmp    1132 <max_nest_depth+0x22>
xchg   %ax,%ax
xor    %edi,%edi
add    $0xffffffff,%ebp
setb   %dil
call   1030 <assert@plt>
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x28,%al
je     1150 <max_nest_depth+0x40>
cmp    $0x29,%al
je     1120 <max_nest_depth+0x10>
test   %al,%al
jne    112e <max_nest_depth+0x1e>
jmp    1160 <max_nest_depth+0x50>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%ebp
cmp    %ebp,%r14d
cmovbe %ebp,%r14d
add    $0x1,%rbx
jmp    1132 <max_nest_depth+0x22>
xor    %edi,%edi
test   %ebp,%ebp
sete   %dil
call   1030 <assert@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @max_nest_depth(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 0, i32* %4
store i64 0, i64* %5
br label %6
6: ; preds = %48, %1
%7 = load i8*, i8** %2
%8 = load i64, i64* %5
%9 = getelementptr inbounds i8, i8* %7, i64 %8
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %51
13: ; preds = %6
%14 = load i8*, i8** %2
%15 = load i64, i64* %5
%16 = getelementptr inbounds i8, i8* %14, i64 %15
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 40
br i1 %19, label %20, label %32
20: ; preds = %13
%21 = load i32, i32* %4
%22 = add i32 %21, 1
store i32 %22, i32* %4
%23 = load i32, i32* %3
%24 = load i32, i32* %4
%25 = icmp ugt i32 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = load i32, i32* %3
br label %30
28: ; preds = %20
%29 = load i32, i32* %4
br label %30
30: ; preds = %28, %26
%31 = phi i32 [ %27, %26 ], [ %29, %28 ]
store i32 %31, i32* %3
br label %47
32: ; preds = %13
%33 = load i8*, i8** %2
%34 = load i64, i64* %5
%35 = getelementptr inbounds i8, i8* %33, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 41
br i1 %38, label %39, label %46
39: ; preds = %32
%40 = load i32, i32* %4
%41 = icmp ugt i32 %40, 0
%42 = zext i1 %41 to i32
%43 = call i32 @assert(i32 %42)
%44 = load i32, i32* %4
%45 = add i32 %44, -1
store i32 %45, i32* %4
br label %46
46: ; preds = %39, %32
br label %47
47: ; preds = %46, %30
br label %48
48: ; preds = %47
%49 = load i64, i64* %5
%50 = add i64 %49, 1
store i64 %50, i64* %5
br label %6
51: ; preds = %6
%52 = load i32, i32* %4
%53 = icmp eq i32 %52, 0
%54 = zext i1 %53 to i32
%55 = call i32 @assert(i32 %54)
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
define i32 @max_nest_depth(i8* nocapture %0) {
br label %2
2: ; preds = %17, %1
%3 = phi i32 [ 0, %1 ], [ %18, %17 ]
%4 = phi i32 [ 0, %1 ], [ %19, %17 ]
%5 = phi i64 [ 0, %1 ], [ %20, %17 ]
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = load i8, i8* %6
switch i8 %7, label %17 [
i8 0, label %21
i8 40, label %8
i8 41, label %12
]
8: ; preds = %2
%9 = add i32 %4, 1
%10 = icmp ugt i32 %3, %9
%11 = select i1 %10, i32 %3, i32 %9
br label %17
12: ; preds = %2
%13 = icmp ne i32 %4, 0
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = add i32 %4, -1
br label %17
17: ; preds = %2, %8, %12
%18 = phi i32 [ %11, %8 ], [ %3, %12 ], [ %3, %2 ]
%19 = phi i32 [ %9, %8 ], [ %16, %12 ], [ %4, %2 ]
%20 = add i64 %5, 1
br label %2
21: ; preds = %2
%22 = icmp eq i32 %4, 0
%23 = zext i1 %22 to i32
%24 = tail call i32 @assert(i32 %23)
ret i32 %3
}