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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ REG_EXTENDED;
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getopt(int, char **, char *);
int /*<<< orphan*/ grep_error(char *);
scalar_t__ optind;
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ regcomp(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
scalar_t__ regexec(int /*<<< orphan*/ *, char *, int, int /*<<< orphan*/ *,
                   int /*<<< orphan*/);
int /*<<< orphan*/ regfree(int /*<<< orphan*/ *);
int /*<<< orphan*/ *stdin;
int strlen(char *);
int main(int argc, char **argv) {
  FILE *fp;
  char line[BUFSIZ];
  regex_t re;
  regmatch_t rm[20];
  int o, ofunc = 0, c = 1;
  if (argc < 2)
    grep_error("Usage: grep [OPTIONS] [PATTERN] [FILE(S)].. \n");
  while ((o = getopt(argc, argv, "o")) != -1)
    switch (o) {
    case 'o':
      ofunc = 1;
      continue;
    case '?':
      grep_error("Invalid option\n");
    }
  argv += optind;
  argc -= optind;
  if ((regcomp(&re, argv[0], REG_EXTENDED)) != 0)
    grep_error("Regex Failed!\n");
  while (c - argc || argc == 1) {
    if (argc == 1)
      fp = stdin;
    else
      fp = fopen(argv[c], "r");
    while ((fgets(line, BUFSIZ, fp)) != NULL) {
      line[strlen(line) - 1] = '\0';
      if (!((regexec(&re, line, 2, rm, 0)) == 0))
        continue;
      if (ofunc != 1) {
        printf("%s\n", line);
        continue;
      }
    }
    ++c;
    if (argc == 1)
      return 0;
  }
  regfree(&re);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x98(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x88(%rbp)
movl   $0x1,-0x8c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1202 <main+0x72>
lea    0xe03(%rip),%rdi
call   1030 <grep_error@plt>
jmp    1207 <main+0x77>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe19(%rip),%rdx
call   10b0 <getopt@plt>
mov    %eax,-0x84(%rbp)
cmp    $0xffffffff,%eax
je     1277 <main+0xe7>
mov    -0x84(%rbp),%eax
mov    %eax,-0x9c(%rbp)
sub    $0x3f,%eax
je     1266 <main+0xd6>
jmp    1243 <main+0xb3>
mov    -0x9c(%rbp),%eax
sub    $0x6f,%eax
jne    1272 <main+0xe2>
jmp    1257 <main+0xc7>
movl   $0x1,-0x88(%rbp)
jmp    1207 <main+0x77>
lea    0xdc3(%rip),%rdi
call   1030 <grep_error@plt>
jmp    1207 <main+0x77>
mov    0x2d62(%rip),%rax
mov    (%rax),%rax
shl    $0x3,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    0x2d4c(%rip),%rax
mov    (%rax),%rcx
movslq -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    0x2d19(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rdi
call   1070 <regcomp@plt>
cmp    $0x0,%rax
je     12d0 <main+0x140>
lea    0xd75(%rip),%rdi
call   1030 <grep_error@plt>
jmp    12d5 <main+0x145>
mov    -0x8c(%rbp),%ecx
sub    -0x8(%rbp),%ecx
mov    $0x1,%al
cmp    $0x0,%ecx
mov    %al,-0x9d(%rbp)
jne    12fc <main+0x16c>
cmpl   $0x1,-0x8(%rbp)
sete   %al
mov    %al,-0x9d(%rbp)
mov    -0x9d(%rbp),%al
test   $0x1,%al
jne    130f <main+0x17f>
jmp    1411 <main+0x281>
cmpl   $0x1,-0x8(%rbp)
jne    132c <main+0x19c>
mov    0x2cc8(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    134b <main+0x1bb>
mov    -0x10(%rbp),%rax
movslq -0x8c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xd0d(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
jmp    1350 <main+0x1c0>
mov    -0x98(%rbp),%rdi
mov    0x2c72(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     13dd <main+0x24d>
mov    -0x98(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x98(%rbp),%rsi
sub    $0x1,%eax
cltq
movb   $0x0,(%rsi,%rax,1)
lea    -0x80(%rbp),%rcx
lea    -0x2c(%rbp),%rdi
mov    $0x2,%edx
xor    %r8d,%r8d
call   1090 <regexec@plt>
cmp    $0x0,%rax
je     13b3 <main+0x223>
jmp    1350 <main+0x1c0>
cmpl   $0x1,-0x88(%rbp)
je     13d8 <main+0x248>
mov    -0x98(%rbp),%rsi
lea    0xc83(%rip),%rdi
call   1060 <printf@plt>
jmp    1350 <main+0x1c0>
jmp    1350 <main+0x1c0>
mov    -0x8c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    140c <main+0x27c>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x90(%rbp)
jmp    142b <main+0x29b>
jmp    12d5 <main+0x145>
lea    -0x2c(%rbp),%rdi
call   1040 <regfree@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x90(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %rsi,%r15
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0x1,%edi
jg     11d5 <main+0x45>
mov    %edi,%r14d
lea    0xe33(%rip),%rdi
call   1030 <grep_error@plt>
mov    %r14d,%edi
mov    $0x1,%r13b
lea    0xe4f(%rip),%r14
lea    0xe4a(%rip),%r12
mov    %edi,-0x2c(%rbp)
jmp    11fb <main+0x6b>
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <grep_error@plt>
mov    -0x2c(%rbp),%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   10b0 <getopt@plt>
cmp    $0x3f,%eax
je     11f0 <main+0x60>
cmp    $0xffffffff,%eax
je     121d <main+0x8d>
cmp    $0x6f,%eax
mov    -0x2c(%rbp),%edi
jne    11fb <main+0x6b>
xor    %r13d,%r13d
jmp    11fb <main+0x6b>
mov    0x2dbc(%rip),%rax
mov    (%rax),%r14
sub    %r14d,-0x2c(%rbp)
mov    (%r15,%r14,8),%rsi
mov    0x2d92(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rdi
call   1070 <regcomp@plt>
test   %rax,%rax
je     1252 <main+0xc2>
lea    0xdf3(%rip),%rdi
call   1030 <grep_error@plt>
lea    (%r15,%r14,8),%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
mov    %rcx,-0x40(%rbp)
mov    $0x1,%ecx
lea    -0x30(%rbp),%r15
lea    -0xa0(%rbp),%r12
not    %r13b
cs nopw 0x0(%rax,%rax,1)
cmp    $0x1,%eax
je     128f <main+0xff>
cmp    -0x40(%rbp),%rcx
je     1340 <main+0x1b0>
cmp    $0x1,%eax
mov    %rcx,-0x48(%rbp)
jne    12b0 <main+0x120>
mov    0x2d49(%rip),%rax
mov    (%rax),%r14
jmp    1311 <main+0x181>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    -0x38(%rbp),%rax
mov    (%rax,%rcx,8),%rdi
lea    0xd90(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r14
jmp    1311 <main+0x181>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x2,%edx
mov    %r12,%rcx
xor    %r8d,%r8d
call   1090 <regexec@plt>
test   %rax,%rax
setne  %al
or     %r13b,%al
test   $0x1,%al
jne    1311 <main+0x181>
lea    0xd48(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
jne    12d0 <main+0x140>
mov    -0x48(%rbp),%rcx
add    $0x1,%rcx
mov    -0x2c(%rbp),%eax
cmp    $0x1,%eax
jne    1280 <main+0xf0>
jmp    1349 <main+0x1b9>
lea    -0x30(%rbp),%rdi
call   1040 <regfree@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [46 x i8] c"Usage: grep [OPTIONS] [PATTERN] [FILE(S)].. \0A\00"
@.str.1 = [2 x i8] c"o\00"
@.str.2 = [16 x i8] c"Invalid option\0A\00"
@optind = global i64 0
@REG_EXTENDED = global i32 0
@.str.3 = [15 x i8] c"Regex Failed!\0A\00"
@stdin = global i32* null
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca [20 x i32]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* @BUFSIZ
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %7
%18 = alloca i8, i64 %16
store i64 %16, i64* %8
store i32 0, i32* %12
store i32 1, i32* %13
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 2
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 @grep_error(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %2
br label %24
24: ; preds = %34, %31, %23
%25 = load i32, i32* %4
%26 = load i8**, i8*** %5
%27 = call i32 @getopt(i32 %25, i8** %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 %27, i32* %11
%28 = icmp ne i32 %27, -1
br i1 %28, label %29, label %35
29: ; preds = %24
%30 = load i32, i32* %11
switch i32 %30, label %34 [
i32 111, label %31
i32 63, label %32
]
31: ; preds = %29
store i32 1, i32* %12
br label %24
32: ; preds = %29
%33 = call i32 @grep_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
br label %24
35: ; preds = %24
%36 = load i64, i64* @optind
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 %36
store i8** %38, i8*** %5
%39 = load i64, i64* @optind
%40 = load i32, i32* %4
%41 = sext i32 %40 to i64
%42 = sub nsw i64 %41, %39
%43 = trunc i64 %42 to i32
store i32 %43, i32* %4
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 0
%46 = load i8*, i8** %45
%47 = load i32, i32* @REG_EXTENDED
%48 = call i64 @regcomp(i32* %9, i8* %46, i32 %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %35
%51 = call i32 @grep_error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %52
52: ; preds = %50, %35
br label %53
53: ; preds = %102, %52
%54 = load i32, i32* %13
%55 = load i32, i32* %4
%56 = sub nsw i32 %54, %55
%57 = icmp ne i32 %56, 0
br i1 %57, label %61, label %58
58: ; preds = %53
%59 = load i32, i32* %4
%60 = icmp eq i32 %59, 1
br label %61
61: ; preds = %58, %53
%62 = phi i1 [ true, %53 ], [ %60, %58 ]
br i1 %62, label %63, label %103
63: ; preds = %61
%64 = load i32, i32* %4
%65 = icmp eq i32 %64, 1
br i1 %65, label %66, label %68
66: ; preds = %63
%67 = load i32*, i32** @stdin
store i32* %67, i32** %6
br label %75
68: ; preds = %63
%69 = load i8**, i8*** %5
%70 = load i32, i32* %13
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8*, i8** %69, i64 %71
%73 = load i8*, i8** %72
%74 = call i32* @fopen(i8* %73, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %74, i32** %6
br label %75
75: ; preds = %68, %66
br label %76
76: ; preds = %95, %93, %89, %75
%77 = load i32, i32* @BUFSIZ
%78 = load i32*, i32** %6
%79 = call i32* @fgets(i8* %18, i32 %77, i32* %78)
%80 = icmp ne i32* %79, null
br i1 %80, label %81, label %96
81: ; preds = %76
%82 = call i32 @strlen(i8* %18)
%83 = sub nsw i32 %82, 1
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i8, i8* %18, i64 %84
store i8 0, i8* %85
%86 = getelementptr inbounds [20 x i32], [20 x i32]* %10, i64 0, i64 0
%87 = call i64 @regexec(i32* %9, i8* %18, i32 2, i32* %86, i32 0)
%88 = icmp eq i64 %87, 0
br i1 %88, label %90, label %89
89: ; preds = %81
br label %76
90: ; preds = %81
%91 = load i32, i32* %12
%92 = icmp ne i32 %91, 1
br i1 %92, label %93, label %95
93: ; preds = %90
%94 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %18)
br label %76
95: ; preds = %90
br label %76
96: ; preds = %76
%97 = load i32, i32* %13
%98 = add nsw i32 %97, 1
store i32 %98, i32* %13
%99 = load i32, i32* %4
%100 = icmp eq i32 %99, 1
br i1 %100, label %101, label %102
101: ; preds = %96
store i32 0, i32* %3
store i32 1, i32* %14
br label %105
102: ; preds = %96
br label %53
103: ; preds = %61
%104 = call i32 @regfree(i32* %9)
store i32 0, i32* %3
store i32 1, i32* %14
br label %105
105: ; preds = %103, %101
%106 = load i8*, i8** %7
%107 = load i32, i32* %3
ret i32 %107
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [46 x i8] c"Usage: grep [OPTIONS] [PATTERN] [FILE(S)].. \0A\00"
@.str.1 = [2 x i8] c"o\00"
@.str.2 = [16 x i8] c"Invalid option\0A\00"
@optind = global i64 0
@REG_EXTENDED = global i32 0
@.str.3 = [15 x i8] c"Regex Failed!\0A\00"
@stdin = global i32* null
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca [20 x i32]
%5 = load i32, i32* @BUFSIZ
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast i32* %3 to i8*
%9 = bitcast [20 x i32]* %4 to i8*
%10 = icmp slt i32 %0, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @grep_error(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
br label %14
14: ; preds = %16, %13
%15 = phi i1 [ true, %13 ], [ false, %16 ]
br label %16
16: ; preds = %18, %14
%17 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
switch i32 %17, label %18 [
i32 -1, label %21
i32 111, label %14
i32 63, label %19
]
18: ; preds = %16, %19
br label %16
19: ; preds = %16
%20 = tail call i32 @grep_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %18
21: ; preds = %16
%22 = load i64, i64* @optind
%23 = getelementptr inbounds i8*, i8** %1, i64 %22
%24 = trunc i64 %22 to i32
%25 = sub i32 %0, %24
%26 = load i8*, i8** %23
%27 = load i32, i32* @REG_EXTENDED
%28 = call i64 @regcomp(i32* %3, i8* %26, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %21
%31 = call i32 @grep_error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %21
%33 = icmp eq i32 %25, 1
%34 = getelementptr inbounds [20 x i32], [20 x i32]* %4, i64 0, i64 0
%35 = zext i32 %25 to i64
br label %36
36: ; preds = %66, %32
%37 = phi i64 [ %67, %66 ], [ 1, %32 ]
%38 = icmp ne i64 %37, %35
%39 = or i1 %33, %38
br i1 %39, label %40, label %68
40: ; preds = %36
br i1 %33, label %41, label %43
41: ; preds = %40
%42 = load i32*, i32** @stdin
br label %47
43: ; preds = %40
%44 = getelementptr inbounds i8*, i8** %23, i64 %37
%45 = load i8*, i8** %44
%46 = call i32* @fopen(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %47
47: ; preds = %43, %41
%48 = phi i32* [ %42, %41 ], [ %46, %43 ]
%49 = load i32, i32* @BUFSIZ
%50 = call i32* @fgets(i8* %7, i32 %49, i32* %48)
%51 = icmp eq i32* %50, null
br i1 %51, label %66, label %52
52: ; preds = %47, %62
%53 = call i32 @strlen(i8* %7)
%54 = add nsw i32 %53, -1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %7, i64 %55
store i8 0, i8* %56
%57 = call i64 @regexec(i32* %3, i8* %7, i32 2, i32* %34, i32 0)
%58 = icmp eq i64 %57, 0
%59 = and i1 %58, %15
br i1 %59, label %60, label %62
60: ; preds = %52
%61 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %7)
br label %62
62: ; preds = %60, %52
%63 = load i32, i32* @BUFSIZ
%64 = call i32* @fgets(i8* %7, i32 %63, i32* %48)
%65 = icmp eq i32* %64, null
br i1 %65, label %66, label %52
66: ; preds = %62, %47
%67 = add nuw i64 %37, 1
br i1 %33, label %70, label %36
68: ; preds = %36
%69 = call i32 @regfree(i32* %3)
br label %70
70: ; preds = %66, %68
ret i32 0
}