#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_7__ TYPE_2__;
typedef struct TYPE_6__ TYPE_1__;

/* Type definitions */
struct TYPE_6__ {
  scalar_t__ rm_eo;
};
typedef TYPE_1__ regmatch_t;
struct TYPE_7__ {
  int re_nsub;
};
typedef TYPE_2__ regex_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
scalar_t__ regexec(TYPE_2__ *, char *, size_t, TYPE_1__ *, int /*<<< orphan*/);
int match_patterns(regex_t *r, FILE *FH) {
  char line[1024];
  int line_no = 1;
  size_t no_sub = r->re_nsub + 1;
  regmatch_t *result;
  char *cp;
  int start = 0;
  if ((result = (regmatch_t *)malloc(sizeof(regmatch_t) * no_sub)) == 0) {
    perror("No more memory - aaaagh! (Die kicking and screaming.)");
    exit(EXIT_FAILURE);
  }
  while ((cp = fgets(line, 1023, FH)) != NULL) {
    while (regexec(r, line + start, no_sub, result, 0) == 0) {
      printf("Line %d: %s", line_no, line);
      start += result->rm_eo;
    }
    line_no++;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<match_patterns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x414(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
cltq
mov    %rax,-0x420(%rbp)
movl   $0x0,-0x434(%rbp)
mov    -0x420(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
jne    11d6 <match_patterns+0x76>
lea    0xe3d(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11db <match_patterns+0x7b>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x3ff,%esi
call   1040 <fgets@plt>
mov    %rax,-0x430(%rbp)
cmp    $0x0,%rax
je     1287 <match_patterns+0x127>
jmp    1206 <match_patterns+0xa6>
mov    -0x8(%rbp),%rdi
lea    -0x410(%rbp),%rsi
movslq -0x434(%rbp),%rax
add    %rax,%rsi
mov    -0x420(%rbp),%rdx
mov    -0x428(%rbp),%rcx
xor    %r8d,%r8d
call   1060 <regexec@plt>
cmp    $0x0,%rax
jne    1273 <match_patterns+0x113>
mov    -0x414(%rbp),%esi
lea    -0x410(%rbp),%rdx
lea    0xde7(%rip),%rdi
call   1030 <printf@plt>
mov    -0x428(%rbp),%rax
mov    (%rax),%rcx
movslq -0x434(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x434(%rbp)
jmp    1206 <match_patterns+0xa6>
mov    -0x414(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x414(%rbp)
jmp    11db <match_patterns+0x7b>
mov    0x2d4a(%rip),%rax
mov    (%rax),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<match_patterns>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rsi,%rbp
mov    %rdi,%r14
movslq (%rdi),%r12
add    $0x1,%r12
lea    0x0(,%r12,8),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1272 <match_patterns+0x112>
mov    %rax,%rbx
lea    0x20(%rsp),%rdi
mov    $0x3ff,%esi
mov    %rbp,0x18(%rsp)
mov    %rbp,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1257 <match_patterns+0xf7>
mov    $0x1,%ebp
xor    %r13d,%r13d
lea    0x20(%rsp),%r15
mov    %r14,0x10(%rsp)
mov    %r12,0x8(%rsp)
jmp    11f4 <match_patterns+0x94>
mov    0x10(%rsp),%r14
mov    0x8(%rsp),%r12
add    $0x1,%ebp
mov    %r15,%rdi
mov    $0x3ff,%esi
mov    0x18(%rsp),%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1257 <match_patterns+0xf7>
movslq %r13d,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x20,%rsi
mov    %r14,%rdi
mov    %r12,%rdx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1060 <regexec@plt>
test   %rax,%rax
jne    11d0 <match_patterns+0x70>
mov    0x10(%rsp),%r14
mov    0x8(%rsp),%r12
nop
lea    0xe0f(%rip),%rdi
mov    %ebp,%esi
mov    %r15,%rdx
call   1030 <printf@plt>
add    (%rbx),%r13d
movslq %r13d,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x20,%rsi
mov    %r14,%rdi
mov    %r12,%rdx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1060 <regexec@plt>
test   %rax,%rax
je     1220 <match_patterns+0xc0>
jmp    11da <match_patterns+0x7a>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd87(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_7__ = type { i32 }
%struct.TYPE_6__ = type { i64 }
@.str = [54 x i8] c"No more memory - aaaagh! (Die kicking and screaming.)\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [12 x i8] c"Line %d: %s\00"
@EXIT_SUCCESS = global i32 0
define i32 @match_patterns(%struct.TYPE_7__* %0, i32* %1) {
%3 = alloca %struct.TYPE_7__*
%4 = alloca i32*
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i64
%8 = alloca %struct.TYPE_6__*
%9 = alloca i8*
%10 = alloca i32
store %struct.TYPE_7__* %0, %struct.TYPE_7__** %3
store i32* %1, i32** %4
store i32 1, i32* %6
%11 = load %struct.TYPE_7__*, %struct.TYPE_7__** %3
%12 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %11, i32 0, i32 0
%13 = load i32, i32* %12
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
store i64 %15, i64* %7
store i32 0, i32* %10
%16 = load i64, i64* %7
%17 = mul i64 8, %16
%18 = trunc i64 %17 to i32
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to %struct.TYPE_6__*
store %struct.TYPE_6__* %20, %struct.TYPE_6__** %8
%21 = icmp eq %struct.TYPE_6__* %20, null
br i1 %21, label %22, label %26
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %2
br label %27
27: ; preds = %54, %26
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%29 = load i32*, i32** %4
%30 = call i8* @fgets(i8* %28, i32 1023, i32* %29)
store i8* %30, i8** %9
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %57
32: ; preds = %27
br label %33
33: ; preds = %43, %32
%34 = load %struct.TYPE_7__*, %struct.TYPE_7__** %3
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i64, i64* %7
%40 = load %struct.TYPE_6__*, %struct.TYPE_6__** %8
%41 = call i64 @regexec(%struct.TYPE_7__* %34, i8* %38, i64 %39, %struct.TYPE_6__* %40, i32 0)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %54
43: ; preds = %33
%44 = load i32, i32* %6
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%46 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %44, i8* %45)
%47 = load %struct.TYPE_6__*, %struct.TYPE_6__** %8
%48 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %47, i32 0, i32 0
%49 = load i64, i64* %48
%50 = load i32, i32* %10
%51 = sext i32 %50 to i64
%52 = add nsw i64 %51, %49
%53 = trunc i64 %52 to i32
store i32 %53, i32* %10
br label %33
54: ; preds = %33
%55 = load i32, i32* %6
%56 = add nsw i32 %55, 1
store i32 %56, i32* %6
br label %27
57: ; preds = %27
%58 = load i32, i32* @EXIT_SUCCESS
ret i32 %58
}

LLVM IR (-O2):
%struct.TYPE_7__ = type { i32 }
%struct.TYPE_6__ = type { i64 }
@.str = [54 x i8] c"No more memory - aaaagh! (Die kicking and screaming.)\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [12 x i8] c"Line %d: %s\00"
@EXIT_SUCCESS = global i32 0
define i32 @match_patterns(%struct.TYPE_7__* %0, i32* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %0, i64 0, i32 0
%6 = load i32, i32* %5
%7 = add nsw i32 %6, 1
%8 = sext i32 %7 to i64
%9 = shl i32 %7, 3
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to %struct.TYPE_6__*
%12 = icmp eq i64 %10, 0
br i1 %12, label %18, label %13
13: ; preds = %2
%14 = call i8* @fgets(i8* %4, i32 1023, i32* %1)
%15 = icmp eq i8* %14, null
br i1 %15, label %44, label %16
16: ; preds = %13
%17 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %11, i64 0, i32 0
br label %22
18: ; preds = %2
%19 = tail call i32 @perror(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %16, %39
%23 = phi i32 [ 0, %16 ], [ %40, %39 ]
%24 = phi i32 [ 1, %16 ], [ %41, %39 ]
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %25
%27 = call i64 @regexec(%struct.TYPE_7__* %0, i8* %26, i64 %8, %struct.TYPE_6__* %11, i32 0)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %39
29: ; preds = %22, %29
%30 = phi i32 [ %34, %29 ], [ %23, %22 ]
%31 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %24, i8* %4)
%32 = load i64, i64* %17
%33 = trunc i64 %32 to i32
%34 = add i32 %30, %33
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %35
%37 = call i64 @regexec(%struct.TYPE_7__* %0, i8* %36, i64 %8, %struct.TYPE_6__* %11, i32 0)
%38 = icmp eq i64 %37, 0
br i1 %38, label %29, label %39
39: ; preds = %29, %22
%40 = phi i32 [ %23, %22 ], [ %34, %29 ]
%41 = add nuw nsw i32 %24, 1
%42 = call i8* @fgets(i8* %4, i32 1023, i32* %1)
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %22
44: ; preds = %39, %13
%45 = load i32, i32* @EXIT_SUCCESS
ret i32 %45
}