#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*/ regex_t;

/* Variables and functions */
int REG_EXTENDED;
int REG_NOSUB;
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int regcomp(int /*<<< orphan*/ *, char *, int);
int regexec(int /*<<< orphan*/ *, char *, size_t, int /*<<< orphan*/ *,
            int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int strlen(char *);
int main() {
  regex_t regex;
  int status;
  char regex_pattern[81], test_string[81];
  printf("Please give the regex expression: ");
  fgets(regex_pattern, 80, stdin);
  regex_pattern[strlen(regex_pattern) - 1] = '\0';
  status = regcomp(&regex, regex_pattern, REG_EXTENDED | REG_NOSUB);
  printf("Validity of regex: %d\n", status);
  if (status)
    return 1;
  while (1) {
    printf("Please give the test string: ");
    fgets(test_string, 80, stdin);
    test_string[strlen(test_string) - 1] = '\0';
    if (strlen(test_string) < 1)
      break;
    status = regexec(&regex, test_string, (size_t)0, NULL, 0);
    printf("Matched %d\n", status);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
mov    $0x50,%esi
call   1060 <fgets@plt>
lea    -0x60(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x60(%rbp,%rax,1)
lea    -0x60(%rbp),%rsi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    0x2e2a(%rip),%rax
or     (%rax),%edx
lea    -0x8(%rbp),%rdi
call   1050 <regcomp@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0xc(%rbp)
je     11e3 <main+0x93>
movl   $0x1,-0x4(%rbp)
jmp    127d <main+0x12d>
jmp    11e8 <main+0x98>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xc0(%rbp),%rdi
mov    0x2de4(%rip),%rax
mov    (%rax),%edx
mov    $0x50,%esi
call   1060 <fgets@plt>
lea    -0xc0(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0xc0(%rbp,%rax,1)
lea    -0xc0(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x1,%eax
jge    1243 <main+0xf3>
jmp    1276 <main+0x126>
lea    -0xc0(%rbp),%rsi
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
mov    %rcx,%rdx
call   1070 <regexec@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e8 <main+0x98>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc8,%rsp
lea    0xe98(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e72(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0x70(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x50,%esi
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    0x2e2f(%rip),%rax
mov    0x2e30(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xc(%rsp),%rdi
mov    %rbx,%rsi
call   1050 <regcomp@plt>
mov    %eax,%ebx
lea    0xe61(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%r14d
test   %ebx,%ebx
je     11ea <main+0x9a>
mov    %r14d,%eax
add    $0xc8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe49(%rip),%r15
lea    0x10(%rsp),%rbx
xor    %r14d,%r14d
lea    0xc(%rsp),%r12
lea    0xe53(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x50,%esi
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    11d5 <main+0x85>
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <regexec@plt>
mov    %r13,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1210 <main+0xc0>

LLVM IR (-O0):
@.str = [35 x i8] c"Please give the regex expression: \00"
@stdin = global i32 0
@REG_EXTENDED = global i32 0
@REG_NOSUB = global i32 0
@.str.1 = [23 x i8] c"Validity of regex: %d\0A\00"
@.str.2 = [30 x i8] c"Please give the test string: \00"
@.str.3 = [12 x i8] c"Matched %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [81 x i8]
%5 = alloca [81 x i8]
store i32 0, i32* %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [81 x i8], [81 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* @stdin
%9 = call i32 @fgets(i8* %7, i32 80, i32 %8)
%10 = getelementptr inbounds [81 x i8], [81 x i8]* %4, i64 0, i64 0
%11 = call i32 @strlen(i8* %10)
%12 = sub nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [81 x i8], [81 x i8]* %4, i64 0, i64 %13
store i8 0, i8* %14
%15 = getelementptr inbounds [81 x i8], [81 x i8]* %4, i64 0, i64 0
%16 = load i32, i32* @REG_EXTENDED
%17 = load i32, i32* @REG_NOSUB
%18 = or i32 %16, %17
%19 = call i32 @regcomp(i32* %2, i8* %15, i32 %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %3
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %0
store i32 1, i32* %1
br label %46
25: ; preds = %0
br label %26
26: ; preds = %25, %40
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%28 = getelementptr inbounds [81 x i8], [81 x i8]* %5, i64 0, i64 0
%29 = load i32, i32* @stdin
%30 = call i32 @fgets(i8* %28, i32 80, i32 %29)
%31 = getelementptr inbounds [81 x i8], [81 x i8]* %5, i64 0, i64 0
%32 = call i32 @strlen(i8* %31)
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [81 x i8], [81 x i8]* %5, i64 0, i64 %34
store i8 0, i8* %35
%36 = getelementptr inbounds [81 x i8], [81 x i8]* %5, i64 0, i64 0
%37 = call i32 @strlen(i8* %36)
%38 = icmp slt i32 %37, 1
br i1 %38, label %39, label %40
39: ; preds = %26
br label %45
40: ; preds = %26
%41 = getelementptr inbounds [81 x i8], [81 x i8]* %5, i64 0, i64 0
%42 = call i32 @regexec(i32* %2, i8* %41, i64 0, i32* null, i32 0)
store i32 %42, i32* %3
%43 = load i32, i32* %3
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %43)
br label %26
45: ; preds = %39
store i32 0, i32* %1
br label %46
46: ; preds = %45, %24
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
@.str = [35 x i8] c"Please give the regex expression: \00"
@stdin = global i32 0
@REG_EXTENDED = global i32 0
@REG_NOSUB = global i32 0
@.str.1 = [23 x i8] c"Validity of regex: %d\0A\00"
@.str.2 = [30 x i8] c"Please give the test string: \00"
@.str.3 = [12 x i8] c"Matched %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [81 x i8]
%3 = alloca [81 x i8]
%4 = bitcast i32* %1 to i8*
%5 = getelementptr inbounds [81 x i8], [81 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [81 x i8], [81 x i8]* %3, i64 0, i64 0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @stdin
%9 = call i32 @fgets(i8* %5, i32 80, i32 %8)
%10 = call i32 @strlen(i8* %5)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [81 x i8], [81 x i8]* %2, i64 0, i64 %12
store i8 0, i8* %13
%14 = load i32, i32* @REG_EXTENDED
%15 = load i32, i32* @REG_NOSUB
%16 = or i32 %15, %14
%17 = call i32 @regcomp(i32* %1, i8* %5, i32 %16)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = icmp eq i32 %17, 0
br i1 %19, label %20, label %33
20: ; preds = %0, %30
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @stdin
%23 = call i32 @fgets(i8* %6, i32 80, i32 %22)
%24 = call i32 @strlen(i8* %6)
%25 = add nsw i32 %24, -1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [81 x i8], [81 x i8]* %3, i64 0, i64 %26
store i8 0, i8* %27
%28 = call i32 @strlen(i8* %6)
%29 = icmp slt i32 %28, 1
br i1 %29, label %33, label %30
30: ; preds = %20
%31 = call i32 @regexec(i32* %1, i8* %6, i64 0, i32* null, i32 0)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %31)
br label %20
33: ; preds = %20, %0
%34 = phi i32 [ 1, %0 ], [ 0, %20 ]
ret i32 %34
}