#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 */
typedef int /*<<< orphan*/ szErrMsg;
typedef int /*<<< orphan*/ regex_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, char const *, ...);
int regcomp(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/);
size_t regerror(int, int /*<<< orphan*/ *, char *, int);
int regexec(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/,
            int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ regfree(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  const char *pRegexStr = NULL;
  const char *pText = NULL;
  regex_t oRegex;
  int nErrCode = 0;
  char szErrMsg[1024] = {0};
  size_t unErrMsgLen = 0;
  if (argc != 3) {
    printf("Usage: %s RegexString Text\n", argv[0]);
    return 1;
  }
  pRegexStr = argv[1];
  pText = argv[2];
  if ((nErrCode = regcomp(&oRegex, pRegexStr, 0)) == 0) {
    if ((nErrCode = regexec(&oRegex, pText, 0, NULL, 0)) == 0) {
      printf("%s matches %s\n", pText, pRegexStr);
      regfree(&oRegex);
      return 0;
    }
  }
  unErrMsgLen = regerror(nErrCode, &oRegex, szErrMsg, sizeof(szErrMsg));
  unErrMsgLen =
      unErrMsgLen < sizeof(szErrMsg) ? unErrMsgLen : sizeof(szErrMsg) - 1;
  szErrMsg[unErrMsgLen] = '\0';
  printf("ErrMsg: %s\n", szErrMsg);
  regfree(&oRegex);
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
movq   $0x0,-0x438(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11d9 <main+0x79>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f5 <main+0x195>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rsi
lea    -0x24(%rbp),%rdi
xor    %edx,%edx
call   1060 <regcomp@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jne    125f <main+0xff>
mov    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdi
xor    %r8d,%r8d
xor    %eax,%eax
mov    %eax,%ecx
mov    %r8d,%edx
call   1080 <regexec@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jne    125a <main+0xfa>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x24(%rbp),%rdi
call   1030 <regfree@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f5 <main+0x195>
jmp    125f <main+0xff>
mov    -0x28(%rbp),%edi
lea    -0x430(%rbp),%rdx
lea    -0x24(%rbp),%rsi
mov    $0x400,%ecx
call   1070 <regerror@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x400,-0x438(%rbp)
jae    12a2 <main+0x142>
mov    -0x438(%rbp),%rax
mov    %rax,-0x440(%rbp)
jmp    12b3 <main+0x153>
mov    $0x3ff,%eax
mov    %rax,-0x440(%rbp)
jmp    12b3 <main+0x153>
mov    -0x440(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x438(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
lea    -0x430(%rbp),%rsi
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x24(%rbp),%rdi
call   1030 <regfree@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x10(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1050 <memset@plt>
cmp    $0x3,%ebp
jne    120c <main+0xac>
mov    0x8(%rbx),%r14
mov    0x10(%rbx),%r15
lea    0xc(%rsp),%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <regcomp@plt>
test   %eax,%eax
jne    11c1 <main+0x61>
xor    %ebx,%ebx
lea    0xc(%rsp),%rdi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <regexec@plt>
test   %eax,%eax
je     1232 <main+0xd2>
lea    0xc(%rsp),%rbx
lea    0x10(%rsp),%rbp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
mov    $0x400,%ecx
call   1070 <regerror@plt>
cmp    $0x3ff,%rax
mov    $0x3ff,%ecx
cmovb  %rax,%rcx
movb   $0x0,0x10(%rsp,%rcx,1)
lea    0xe33(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <regfree@plt>
jmp    121d <main+0xbd>
mov    (%rbx),%rsi
lea    0xdea(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde3(%rip),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xc(%rsp),%rdi
call   1030 <regfree@plt>
jmp    1222 <main+0xc2>

LLVM IR (-O0):
@.str = [28 x i8] c"Usage: %s RegexString Text\0A\00"
@.str.1 = [15 x i8] c"%s matches %s\0A\00"
@.str.2 = [12 x i8] c"ErrMsg: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1024 x i8]
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i8* null, i8** %7
store i32 0, i32* %9
%12 = bitcast [1024 x i8]* %10 to i8*
store i64 0, i64* %11
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 1, i32* %3
br label %56
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
store i8* %23, i8** %6
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
store i8* %26, i8** %7
%27 = load i8*, i8** %6
%28 = call i32 @regcomp(i32* %8, i8* %27, i32 0)
store i32 %28, i32* %9
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %40
30: ; preds = %20
%31 = load i8*, i8** %7
%32 = call i32 @regexec(i32* %8, i8* %31, i32 0, i32* null, i32 0)
store i32 %32, i32* %9
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = load i8*, i8** %7
%36 = load i8*, i8** %6
%37 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %35, i8* %36)
%38 = call i32 @regfree(i32* %8)
store i32 0, i32* %3
br label %56
39: ; preds = %30
br label %40
40: ; preds = %39, %20
%41 = load i32, i32* %9
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%43 = call i64 @regerror(i32 %41, i32* %8, i8* %42, i32 1024)
store i64 %43, i64* %11
%44 = load i64, i64* %11
%45 = icmp ult i64 %44, 1024
br i1 %45, label %46, label %48
46: ; preds = %40
%47 = load i64, i64* %11
br label %49
48: ; preds = %40
br label %49
49: ; preds = %48, %46
%50 = phi i64 [ %47, %46 ], [ 1023, %48 ]
store i64 %50, i64* %11
%51 = load i64, i64* %11
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 %51
store i8 0, i8* %52
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%54 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %53)
%55 = call i32 @regfree(i32* %8)
store i32 1, i32* %3
br label %56
56: ; preds = %49, %34, %15
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@.str = [28 x i8] c"Usage: %s RegexString Text\0A\00"
@.str.1 = [15 x i8] c"%s matches %s\0A\00"
@.str.2 = [12 x i8] c"ErrMsg: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = bitcast i32* %3 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 3
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %32
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = call i32 @regcomp(i32* %3, i8* %13, i32 0)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %11
%19 = call i32 @regexec(i32* %3, i8* %15, i32 0, i32* null, i32 0)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %15, i8* %13)
%23 = call i32 @regfree(i32* %3)
br label %32
24: ; preds = %18, %11
%25 = phi i32 [ %19, %18 ], [ %16, %11 ]
%26 = call i64 @regerror(i32 %25, i32* %3, i8* %6, i32 1024)
%27 = icmp ult i64 %26, 1023
%28 = select i1 %27, i64 %26, i64 1023
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %28
store i8 0, i8* %29
%30 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%31 = call i32 @regfree(i32* %3)
br label %32
32: ; preds = %24, %21, %8
%33 = phi i32 [ 1, %8 ], [ 0, %21 ], [ 1, %24 ]
ret i32 %33
}