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

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ match_patterns(char *, int /*<<< orphan*/ *);
int regcomp(char *, char *, int /*<<< orphan*/);
size_t regerror(int, char *, char *, size_t);
int /*<<< orphan*/ regfree(char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int do_regex(regex_t *r, char *p, char *f) {
  int err_no = 0;
  FILE *FH = NULL;
  if ((err_no = regcomp(r, p, 0)) != 0) {
    size_t length;
    char *buffer;
    length = regerror(err_no, r, NULL, 0);
    buffer = malloc(length);
    regerror(err_no, r, buffer, length);
    fprintf(stderr, "%s\n", buffer);
    free(buffer);
    regfree(r);
    return EXIT_FAILURE;
  }
  if ((FH = fopen(f, "r")) == NULL) {
    fprintf(stderr, "Couldn't open file %s; %s\n", f, strerror(errno));
    exit(EXIT_FAILURE);
  }
  match_patterns(r, FH);
  regfree(r);
  free(r);
  fclose(FH);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<do_regex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1070 <regcomp@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     1264 <do_regex+0xb4>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1090 <regerror@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%rcx
call   1090 <regerror@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1050 <regfree@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12fe <do_regex+0x14e>
mov    -0x20(%rbp),%rdi
lea    0xd91(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    12ca <do_regex+0x11a>
mov    0x2d57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd4d(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <match_patterns@plt>
mov    -0x10(%rbp),%rdi
call   1050 <regfree@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_regex>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdi,%r15
xor    %edx,%edx
call   1070 <regcomp@plt>
test   %eax,%eax
je     1229 <do_regex+0x79>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <regerror@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   10a0 <malloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rax,%rdx
mov    %r14,%rcx
call   1090 <regerror@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <regfree@plt>
mov    0x2da9(%rip),%rax
jmp    126a <do_regex+0xba>
lea    0xdd0(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1277 <do_regex+0xc7>
mov    %rax,%rbp
mov    %r15,%rdi
mov    %rax,%rsi
call   1060 <match_patterns@plt>
mov    %r15,%rdi
call   1050 <regfree@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d62(%rip),%rax
mov    (%rax),%ebx
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd6d(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [27 x i8] c"Couldn't open file %s; %s\0A\00"
@errno = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @do_regex(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 0, i32* %8
store i32* null, i32** %9
%12 = load i8*, i8** %5
%13 = load i8*, i8** %6
%14 = call i32 @regcomp(i8* %12, i8* %13, i32 0)
store i32 %14, i32* %8
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %35
16: ; preds = %3
%17 = load i32, i32* %8
%18 = load i8*, i8** %5
%19 = call i64 @regerror(i32 %17, i8* %18, i8* null, i64 0)
store i64 %19, i64* %10
%20 = load i64, i64* %10
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %11
%22 = load i32, i32* %8
%23 = load i8*, i8** %5
%24 = load i8*, i8** %11
%25 = load i64, i64* %10
%26 = call i64 @regerror(i32 %22, i8* %23, i8* %24, i64 %25)
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %11
%29 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = load i8*, i8** %11
%31 = call i32 @free(i8* %30)
%32 = load i8*, i8** %5
%33 = call i32 @regfree(i8* %32)
%34 = load i32, i32* @EXIT_FAILURE
store i32 %34, i32* %4
br label %58
35: ; preds = %3
%36 = load i8*, i8** %7
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %37, i32** %9
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %47
39: ; preds = %35
%40 = load i32, i32* @stderr
%41 = load i8*, i8** %7
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %41, i8* %43)
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %35
%48 = load i8*, i8** %5
%49 = load i32*, i32** %9
%50 = call i32 @match_patterns(i8* %48, i32* %49)
%51 = load i8*, i8** %5
%52 = call i32 @regfree(i8* %51)
%53 = load i8*, i8** %5
%54 = call i32 @free(i8* %53)
%55 = load i32*, i32** %9
%56 = call i32 @fclose(i32* %55)
%57 = load i32, i32* @EXIT_SUCCESS
store i32 %57, i32* %4
br label %58
58: ; preds = %47, %16
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [27 x i8] c"Couldn't open file %s; %s\0A\00"
@errno = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @do_regex(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @regcomp(i8* %0, i8* %1, i32 0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %14, label %6
6: ; preds = %3
%7 = tail call i64 @regerror(i32 %4, i8* %0, i8* null, i64 0)
%8 = tail call i8* @malloc(i64 %7)
%9 = tail call i64 @regerror(i32 %4, i8* %0, i8* %8, i64 %7)
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %8)
%12 = tail call i32 @free(i8* %8)
%13 = tail call i32 @regfree(i8* %0)
br label %29
14: ; preds = %3
%15 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %2, i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %14
%25 = tail call i32 @match_patterns(i8* %0, i32* %15)
%26 = tail call i32 @regfree(i8* %0)
%27 = tail call i32 @free(i8* %0)
%28 = tail call i32 @fclose(i32* %15)
br label %29
29: ; preds = %24, %6
%30 = phi i32* [ @EXIT_FAILURE, %6 ], [ @EXIT_SUCCESS, %24 ]
%31 = load i32, i32* %30
ret i32 %31
}