#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 */
size_t strlen(char const *);
size_t find_next_comma_in_args(const char *buf) {
  const size_t buf_len = strlen(buf);
  size_t len = 0;
  int paren = 0;
  while (len < buf_len) {
    if ((')' == buf[len]) && (0 == paren)) {
      return len;
    }
    paren += ('(' == buf[len]);
    paren -= (')' == buf[len]);
    if ((',' == buf[len]) && (0 == paren)) {
      return len;
    }
    len++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<find_next_comma_in_args>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11fd <find_next_comma_in_args+0xed>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x29,%eax
cmp    %ecx,%eax
jne    1177 <find_next_comma_in_args+0x67>
xor    %eax,%eax
cmp    -0x24(%rbp),%eax
jne    1177 <find_next_comma_in_args+0x67>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1205 <find_next_comma_in_args+0xf5>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x28,%eax
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x29,%eax
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x2c,%eax
cmp    %ecx,%eax
jne    11ec <find_next_comma_in_args+0xdc>
xor    %eax,%eax
cmp    -0x24(%rbp),%eax
jne    11ec <find_next_comma_in_args+0xdc>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1205 <find_next_comma_in_args+0xf5>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1138 <find_next_comma_in_args+0x28>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_next_comma_in_args>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %rax,%rax
je     1168 <find_next_comma_in_args+0x58>
mov    %rax,%rcx
xor    %edx,%edx
xor    %eax,%eax
jmp    1139 <find_next_comma_in_args+0x29>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rax
cmp    %rax,%rcx
je     1168 <find_next_comma_in_args+0x58>
movzbl (%r14,%rax,1),%ebx
cmp    $0x29,%bl
jne    1147 <find_next_comma_in_args+0x37>
test   %edx,%edx
je     116a <find_next_comma_in_args+0x5a>
xor    %esi,%esi
cmp    $0x29,%bl
sete   %sil
xor    %edi,%edi
cmp    $0x28,%bl
sete   %dil
add    %edi,%edx
sub    %esi,%edx
cmp    $0x2c,%bl
jne    1130 <find_next_comma_in_args+0x20>
test   %edx,%edx
jne    1130 <find_next_comma_in_args+0x20>
jmp    116a <find_next_comma_in_args+0x5a>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @find_next_comma_in_args(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %4
store i64 0, i64* %5
store i32 0, i32* %6
br label %9
9: ; preds = %55, %1
%10 = load i64, i64* %5
%11 = load i64, i64* %4
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %58
13: ; preds = %9
%14 = load i8*, i8** %3
%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 41, %18
br i1 %19, label %20, label %25
20: ; preds = %13
%21 = load i32, i32* %6
%22 = icmp eq i32 0, %21
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = load i64, i64* %5
store i64 %24, i64* %2
br label %59
25: ; preds = %20, %13
%26 = load i8*, i8** %3
%27 = load i64, i64* %5
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 40, %30
%32 = zext i1 %31 to i32
%33 = load i32, i32* %6
%34 = add nsw i32 %33, %32
store i32 %34, i32* %6
%35 = load i8*, i8** %3
%36 = load i64, i64* %5
%37 = getelementptr inbounds i8, i8* %35, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 41, %39
%41 = zext i1 %40 to i32
%42 = load i32, i32* %6
%43 = sub nsw i32 %42, %41
store i32 %43, i32* %6
%44 = load i8*, i8** %3
%45 = load i64, i64* %5
%46 = getelementptr inbounds i8, i8* %44, i64 %45
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 44, %48
br i1 %49, label %50, label %55
50: ; preds = %25
%51 = load i32, i32* %6
%52 = icmp eq i32 0, %51
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = load i64, i64* %5
store i64 %54, i64* %2
br label %59
55: ; preds = %50, %25
%56 = load i64, i64* %5
%57 = add i64 %56, 1
store i64 %57, i64* %5
br label %9
58: ; preds = %9
store i64 0, i64* %2
br label %59
59: ; preds = %58, %53, %23
%60 = load i64, i64* %2
ret i64 %60
}

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