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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
scalar_t__ match(char const *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc != 3) {
    fprintf(stderr, "usage: %s regex filename\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  const char *regexp = argv[1];
  FILE *f = fopen(argv[2], "r");
  if (f == NULL) {
    fprintf(stderr, "error opening file '%s': %s\n", argv[2], strerror(errno));
    exit(EXIT_FAILURE);
  }
  char *line = NULL;
  size_t length = 0;
  while (getline(&line, &length, f) != -1) {
    if (match(regexp, line))
      fprintf(stdout, "%s", line);
  }
  free(line);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11cc <main+0x4c>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe33(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1247 <main+0xc7>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <getline@plt>
cmp    $0xffffffff,%eax
je     12a8 <main+0x128>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <match@plt>
cmp    $0x0,%rax
je     12a3 <main+0x123>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xd9d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1257 <main+0xd7>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
cmp    $0x3,%edi
jne    125c <main+0xdc>
mov    0x8(%r15),%r14
mov    0x10(%r15),%rdi
lea    0xe71(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1278 <main+0xf8>
mov    %rax,%rbx
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
call   1080 <getline@plt>
cmp    $0xffffffff,%eax
je     1239 <main+0xb9>
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r12
mov    0x2dd4(%rip),%rbp
lea    0xe3e(%rip),%r13
jmp    1213 <main+0x93>
nopl   (%rax)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1080 <getline@plt>
cmp    $0xffffffff,%eax
je     1239 <main+0xb9>
mov    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1060 <match@plt>
test   %rax,%rax
je     1200 <main+0x80>
mov    0x0(%rbp),%edi
mov    0x8(%rsp),%rdx
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1200 <main+0x80>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
mov    (%r15),%rdx
lea    0xd91(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12a9 <main+0x129>
mov    0x2d61(%rip),%rax
mov    (%rax),%ebx
mov    0x10(%r15),%r14
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd82(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"usage: %s regex filename\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [29 x i8] c"error opening file '%s': %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.3 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %20
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
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
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %7
%28 = load i32*, i32** %7
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %40
30: ; preds = %20
%31 = load i32, i32* @stderr
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %34, i8* %36)
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %20
store i8* null, i8** %8
store i64 0, i64* %9
br label %41
41: ; preds = %54, %40
%42 = load i32*, i32** %7
%43 = call i32 @getline(i8** %8, i64* %9, i32* %42)
%44 = icmp ne i32 %43, -1
br i1 %44, label %45, label %55
45: ; preds = %41
%46 = load i8*, i8** %6
%47 = load i8*, i8** %8
%48 = call i64 @match(i8* %46, i8* %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %45
%51 = load i32, i32* @stdout
%52 = load i8*, i8** %8
%53 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %50, %45
br label %41
55: ; preds = %41
%56 = load i8*, i8** %8
%57 = call i32 @free(i8* %56)
%58 = load i32*, i32** %7
%59 = call i32 @fclose(i32* %58)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"usage: %s regex filename\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [29 x i8] c"error opening file '%s': %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.3 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = icmp eq i32 %0, 3
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %27
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %15
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %21, i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %12
%28 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%29 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%30 = call i32 @getline(i8** %3, i64* %4, i32* %17)
%31 = icmp eq i32 %30, -1
br i1 %31, label %43, label %32
32: ; preds = %27, %40
%33 = load i8*, i8** %3
%34 = call i64 @match(i8* %14, i8* %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %32
%37 = load i32, i32* @stdout
%38 = load i8*, i8** %3
%39 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %36, %32
%41 = call i32 @getline(i8** %3, i64* %4, i32* %17)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %32
43: ; preds = %40, %27
%44 = load i8*, i8** %3
%45 = call i32 @free(i8* %44)
%46 = call i32 @fclose(i32* %17)
ret i32 0
}