#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*/ free(char *);
char *regexp(char *, char const *, int *, int *);
char *strdup(char const *);
int RegExtractAll(const char *string, const char *pattern, char **buffer) {
  int counter = 0;
  int cnt = 0;
  char *myeditableString = strdup(string);
  while (1) {
    int a, b;
    char *match = regexp(myeditableString, pattern, &a, &b);
    if (match != NULL) {
      buffer[counter++] = strdup(match);
      free(match);
      cnt++;
    } else {
      break;
    }
    myeditableString += b;
  }
  return cnt;
}


x86 assembly (clang, -O0):
<RegExtractAll>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1050 <regexp@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11bc <RegExtractAll+0x8c>
mov    -0x38(%rbp),%rdi
call   1040 <strdup@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11c1 <RegExtractAll+0x91>
jmp    11d7 <RegExtractAll+0xa7>
mov    -0x30(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    115f <RegExtractAll+0x2f>
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RegExtractAll>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
call   1040 <strdup@plt>
mov    %rax,%r12
lea    0x4(%rsp),%rdx
mov    %rsp,%rcx
mov    %rax,%rdi
mov    %r15,%rsi
call   1050 <regexp@plt>
test   %rax,%rax
je     11ac <RegExtractAll+0x7c>
mov    %rax,%r13
xor    %ebx,%ebx
mov    %rsp,%rbp
nopl   0x0(%rax)
mov    %r13,%rdi
call   1040 <strdup@plt>
mov    %rax,(%r14,%rbx,8)
add    $0x1,%rbx
mov    %r13,%rdi
call   1030 <free@plt>
movslq (%rsp),%rax
add    %rax,%r12
mov    %r12,%rdi
mov    %r15,%rsi
lea    0x4(%rsp),%rdx
mov    %rbp,%rcx
call   1050 <regexp@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1170 <RegExtractAll+0x40>
jmp    11ae <RegExtractAll+0x7e>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @RegExtractAll(i8* nocapture %0, i8* %1, i8** nocapture writeonly %2) {
%4 = alloca i32
%5 = alloca i32
%6 = tail call i8* @strdup(i8* %0)
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = call i8* @regexp(i8* %6, i8* %1, i32* %4, i32* %5)
%10 = icmp eq i8* %9, null
br i1 %10, label %26, label %11
11: ; preds = %3, %11
%12 = phi i64 [ %16, %11 ], [ 0, %3 ]
%13 = phi i8* [ %22, %11 ], [ %9, %3 ]
%14 = phi i8* [ %21, %11 ], [ %6, %3 ]
%15 = call i8* @strdup(i8* %13)
%16 = add nuw i64 %12, 1
%17 = getelementptr inbounds i8*, i8** %2, i64 %12
store i8* %15, i8** %17
%18 = call i32 @free(i8* %13)
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %14, i64 %20
%22 = call i8* @regexp(i8* %21, i8* %1, i32* %4, i32* %5)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %11
24: ; preds = %11
%25 = trunc i64 %16 to i32
br label %26
26: ; preds = %24, %3
%27 = phi i32 [ 0, %3 ], [ %25, %24 ]
ret i32 %27
}