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

/* Variables and functions */
int /*<<< orphan*/ feof(int /*<<< orphan*/);
scalar_t__ ferror(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ rgrep_matches(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <PATTERN>\n", argv[0]);
    return 2;
  }
  char buf[4096];
  while (!feof(stdin) && !ferror(stdin)) {
    if (!fgets(buf, sizeof(buf), stdin)) {
      break;
    }
    if (rgrep_matches(buf, argv[1])) {
      fputs(buf, stdout);
      fflush(stdout);
    }
  }
  if (ferror(stdin)) {
    perror(argv[0]);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11cb <main+0x4b>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe46(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12c6 <main+0x146>
jmp    11d0 <main+0x50>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1070 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1011(%rbp)
jne    120e <main+0x8e>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
mov    %al,-0x1011(%rbp)
mov    -0x1011(%rbp),%al
test   $0x1,%al
jne    1221 <main+0xa1>
jmp    128f <main+0x10f>
lea    -0x1010(%rbp),%rdi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1050 <fgets@plt>
cmp    $0x0,%eax
jne    1249 <main+0xc9>
jmp    128f <main+0x10f>
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1080 <rgrep_matches@plt>
cmp    $0x0,%rax
je     128a <main+0x10a>
lea    -0x1010(%rbp),%rdi
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
jmp    11d0 <main+0x50>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12bf <main+0x13f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c6 <main+0x146>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jne    11c8 <main+0x48>
mov    0x2e4b(%rip),%rbx
mov    (%rbx),%edi
call   1070 <feof@plt>
test   %eax,%eax
je     11f4 <main+0x74>
mov    (%rbx),%edi
call   1030 <ferror@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     11e5 <main+0x65>
mov    (%r14),%rdi
call   10a0 <perror@plt>
mov    $0x1,%eax
jmp    11e5 <main+0x65>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe25(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x2,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rsp,%r15
mov    0x2dd2(%rip),%r12
jmp    120b <main+0x8b>
mov    (%rbx),%edi
call   1070 <feof@plt>
test   %eax,%eax
jne    11a8 <main+0x28>
mov    (%rbx),%edi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11a8 <main+0x28>
mov    (%rbx),%edx
mov    %r15,%rdi
mov    $0x1000,%esi
call   1050 <fgets@plt>
test   %eax,%eax
je     11a8 <main+0x28>
mov    0x8(%r14),%rsi
mov    %r15,%rdi
call   1080 <rgrep_matches@plt>
test   %rax,%rax
je     1200 <main+0x80>
mov    (%r12),%esi
mov    %r15,%rdi
call   1040 <fputs@plt>
mov    (%r12),%edi
call   1090 <fflush@plt>
jmp    1200 <main+0x80>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"Usage: %s <PATTERN>\0A\00"
@stdin = global i32 0
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 2
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 2, i32* %3
br label %57
15: ; preds = %2
br label %16
16: ; preds = %46, %15
%17 = load i32, i32* @stdin
%18 = call i32 @feof(i32 %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %16
%21 = load i32, i32* @stdin
%22 = call i64 @ferror(i32 %21)
%23 = icmp ne i64 %22, 0
%24 = xor i1 %23, true
br label %25
25: ; preds = %20, %16
%26 = phi i1 [ false, %16 ], [ %24, %20 ]
br i1 %26, label %27, label %47
27: ; preds = %25
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%29 = load i32, i32* @stdin
%30 = call i32 @fgets(i8* %28, i32 4096, i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %27
br label %47
33: ; preds = %27
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i64 @rgrep_matches(i8* %34, i8* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %33
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%42 = load i32, i32* @stdout
%43 = call i32 @fputs(i8* %41, i32 %42)
%44 = load i32, i32* @stdout
%45 = call i32 @fflush(i32 %44)
br label %46
46: ; preds = %40, %33
br label %16
47: ; preds = %32, %25
%48 = load i32, i32* @stdin
%49 = call i64 @ferror(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %47
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 0
%54 = load i8*, i8** %53
%55 = call i32 @perror(i8* %54)
store i32 1, i32* %3
br label %57
56: ; preds = %47
store i32 0, i32* %3
br label %57
57: ; preds = %56, %51, %9
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"Usage: %s <PATTERN>\0A\00"
@stdin = global i32 0
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = icmp eq i32 %0, 2
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %46
9: ; preds = %2
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%11 = load i32, i32* @stdin
%12 = call i32 @feof(i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %37
14: ; preds = %9
%15 = getelementptr inbounds i8*, i8** %1, i64 1
br label %16
16: ; preds = %14, %33
%17 = load i32, i32* @stdin
%18 = call i64 @ferror(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %37
20: ; preds = %16
%21 = load i32, i32* @stdin
%22 = call i32 @fgets(i8* %10, i32 4096, i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %37, label %24
24: ; preds = %20
%25 = load i8*, i8** %15
%26 = call i64 @rgrep_matches(i8* %10, i8* %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %24
%29 = load i32, i32* @stdout
%30 = call i32 @fputs(i8* %10, i32 %29)
%31 = load i32, i32* @stdout
%32 = call i32 @fflush(i32 %31)
br label %33
33: ; preds = %28, %24
%34 = load i32, i32* @stdin
%35 = call i32 @feof(i32 %34)
%36 = icmp eq i32 %35, 0
br i1 %36, label %16, label %37
37: ; preds = %16, %20, %33, %9
%38 = load i32, i32* @stdin
%39 = call i64 @ferror(i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %37
%42 = load i8*, i8** %1
%43 = call i32 @perror(i8* %42)
br label %44
44: ; preds = %37, %41
%45 = phi i32 [ 1, %41 ], [ 0, %37 ]
br label %46
46: ; preds = %44, %5
%47 = phi i32 [ 2, %5 ], [ %45, %44 ]
ret i32 %47
}