#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int /*<<< orphan*/ rm_eo;
};
typedef TYPE_1__ regmatch_t;
typedef int /*<<< orphan*/ regex_t;

/* Variables and functions */
scalar_t__ regcomp(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/);
int regexec(int /*<<< orphan*/ *, char const *, int, TYPE_1__ *,
            int /*<<< orphan*/);
int count_in_str(const char *substr, const char *str) {
  int count = 0;
  int nomatch;
  regex_t exp;
  regmatch_t matches[1];
  if (regcomp(&exp, substr, 0) != 0) {
    return -1;
  }
  while (1) {
    nomatch = regexec(&exp, str, 1, matches, 0);
    if (nomatch) {
      break;
    }
    str += matches[0].rm_eo;
    count++;
  }
  return count;
}


x86 assembly (clang, -O0):
<count_in_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
lea    -0x24(%rbp),%rdi
xor    %edx,%edx
call   1030 <regcomp@plt>
cmp    $0x0,%rax
je     115c <count_in_str+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <count_in_str+0x91>
jmp    1161 <count_in_str+0x41>
mov    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rcx
lea    -0x24(%rbp),%rdi
mov    $0x1,%edx
xor    %r8d,%r8d
call   1040 <regexec@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     118c <count_in_str+0x6c>
jmp    11ab <count_in_str+0x8b>
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1161 <count_in_str+0x41>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_in_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%rsi
lea    0x4(%rsp),%rdi
xor    %edx,%edx
call   1030 <regcomp@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    1194 <count_in_str+0x74>
lea    0x4(%rsp),%rdi
mov    %rsp,%rcx
mov    %rbx,%rsi
mov    $0x1,%edx
xor    %r8d,%r8d
call   1040 <regexec@plt>
mov    $0x0,%ebp
test   %eax,%eax
jne    1194 <count_in_str+0x74>
xor    %ebp,%ebp
lea    0x4(%rsp),%r14
mov    %rsp,%r15
xchg   %ax,%ax
movslq (%rsp),%rax
add    %rax,%rbx
add    $0x1,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
mov    %r15,%rcx
xor    %r8d,%r8d
call   1040 <regexec@plt>
test   %eax,%eax
je     1170 <count_in_str+0x50>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32 }
define i32 @count_in_str(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca [1 x %struct.TYPE_3__]
%5 = bitcast i32* %3 to i8*
%6 = bitcast [1 x %struct.TYPE_3__]* %4 to i8*
%7 = call i64 @regcomp(i32* %3, i8* %0, i32 0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %24
9: ; preds = %2
%10 = getelementptr inbounds [1 x %struct.TYPE_3__], [1 x %struct.TYPE_3__]* %4, i64 0, i64 0
%11 = call i32 @regexec(i32* %3, i8* %1, i32 1, %struct.TYPE_3__* %10, i32 0)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %24
13: ; preds = %9
%14 = getelementptr inbounds [1 x %struct.TYPE_3__], [1 x %struct.TYPE_3__]* %4, i64 0, i64 0, i32 0
br label %15
15: ; preds = %13, %15
%16 = phi i32 [ 0, %13 ], [ %21, %15 ]
%17 = phi i8* [ %1, %13 ], [ %20, %15 ]
%18 = load i32, i32* %14
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = add nuw nsw i32 %16, 1
%22 = call i32 @regexec(i32* %3, i8* %20, i32 1, %struct.TYPE_3__* %10, i32 0)
%23 = icmp eq i32 %22, 0
br i1 %23, label %15, label %24
24: ; preds = %15, %9, %2
%25 = phi i32 [ -1, %2 ], [ 0, %9 ], [ %21, %15 ]
ret i32 %25
}