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

/* Variables and functions */
int REG_EXTENDED;
int REG_NEWLINE;
int REG_NOSUB;
int /*<<< orphan*/ do_grep(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int regcomp(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ regerror(int, int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ regfree(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
int main(int argc, char *argv[]) {
  regex_t pat;
  int err;
  int i;
  if (argc < 2) {
    fputs("no pattern\n", stderr);
    exit(1);
  }
  err = regcomp(&pat, argv[1], REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
  if (err != 0) {
    char buf[4096];
    regerror(err, &pat, buf, sizeof buf);
    puts(buf);
    exit(1);
  }
  if (argc == 2) {
    do_grep(&pat, stdin);
    regfree(&pat);
    exit(0);
  }
  for (i = 2; i < argc; i++) {
    FILE *f;
    f = fopen(argv[i], "r");
    if (!f) {
      perror(argv[i]);
      exit(1);
    }
    do_grep(&pat, f);
    fclose(f);
  }
  regfree(&pat);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11e2 <main+0x42>
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
lea    0xe2d(%rip),%rdi
call   1060 <fputs@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
mov    0x2dd6(%rip),%rax
or     (%rax),%edx
mov    0x2ddd(%rip),%rax
or     (%rax),%edx
lea    -0x14(%rbp),%rdi
call   1070 <regcomp@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1249 <main+0xa9>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rdx
lea    -0x14(%rbp),%rsi
mov    $0x1000,%ecx
call   1080 <regerror@plt>
lea    -0x1020(%rbp),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jne    1276 <main+0xd6>
mov    0x2d8e(%rip),%rax
mov    (%rax),%rsi
lea    -0x14(%rbp),%rdi
call   1090 <do_grep@plt>
lea    -0x14(%rbp),%rdi
call   1050 <regfree@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
movl   $0x2,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12fb <main+0x15b>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xd70(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jne    12d1 <main+0x131>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x1028(%rbp),%rsi
lea    -0x14(%rbp),%rdi
call   1090 <do_grep@plt>
mov    -0x1028(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    127d <main+0xdd>
lea    -0x14(%rbp),%rdi
call   1050 <regfree@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
cmp    $0x1,%edi
jg     11d5 <main+0x35>
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
lea    0xe3a(%rip),%rdi
call   1060 <fputs@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    %rsi,%r12
mov    %edi,%ebp
mov    0x2ddf(%rip),%rax
mov    0x2de8(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x8(%rsi),%rsi
mov    0x2de9(%rip),%rax
or     (%rax),%edx
lea    0xc(%rsp),%rdi
call   1070 <regcomp@plt>
test   %eax,%eax
jne    1228 <main+0x88>
cmp    $0x2,%ebp
jne    1253 <main+0xb3>
mov    0x2dd5(%rip),%rax
mov    (%rax),%rsi
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <do_grep@plt>
mov    %rbx,%rdi
jmp    12a5 <main+0x105>
lea    0xc(%rsp),%rsi
lea    0x10(%rsp),%rbx
mov    %eax,%edi
mov    %rbx,%rdx
mov    $0x1000,%ecx
call   1080 <regerror@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    %ebp,%r13d
mov    $0x2,%ebx
lea    0xdaa(%rip),%r14
lea    0xc(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r12,%rbx,8),%rdi
mov    %r14,%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12b1 <main+0x111>
mov    %rax,%rbp
mov    %r15,%rdi
mov    %rax,%rsi
call   1090 <do_grep@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1270 <main+0xd0>
lea    0xc(%rsp),%rdi
call   1050 <regfree@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    %ebx,%eax
mov    (%r12,%rax,8),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"no pattern\0A\00"
@stderr = global i32 0
@REG_EXTENDED = global i32 0
@REG_NOSUB = global i32 0
@REG_NEWLINE = global i32 0
@stdin = global i32* null
@.str.1 = [2 x i8] c"r\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [4096 x i8]
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @REG_EXTENDED
%22 = load i32, i32* @REG_NOSUB
%23 = or i32 %21, %22
%24 = load i32, i32* @REG_NEWLINE
%25 = or i32 %23, %24
%26 = call i32 @regcomp(i32* %6, i8* %20, i32 %25)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %17
%30 = load i32, i32* %7
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%32 = call i32 @regerror(i32 %30, i32* %6, i8* %31, i32 4096)
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%34 = call i32 @puts(i8* %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %17
%37 = load i32, i32* %4
%38 = icmp eq i32 %37, 2
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = load i32*, i32** @stdin
%41 = call i32 @do_grep(i32* %6, i32* %40)
%42 = call i32 @regfree(i32* %6)
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %36
store i32 2, i32* %8
br label %45
45: ; preds = %71, %44
%46 = load i32, i32* %8
%47 = load i32, i32* %4
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %74
49: ; preds = %45
%50 = load i8**, i8*** %5
%51 = load i32, i32* %8
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8*, i8** %50, i64 %52
%54 = load i8*, i8** %53
%55 = call i32* @fopen(i8* %54, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %55, i32** %10
%56 = load i32*, i32** %10
%57 = icmp ne i32* %56, null
br i1 %57, label %66, label %58
58: ; preds = %49
%59 = load i8**, i8*** %5
%60 = load i32, i32* %8
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8*, i8** %59, i64 %61
%63 = load i8*, i8** %62
%64 = call i32 @perror(i8* %63)
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %49
%67 = load i32*, i32** %10
%68 = call i32 @do_grep(i32* %6, i32* %67)
%69 = load i32*, i32** %10
%70 = call i32 @fclose(i32* %69)
br label %71
71: ; preds = %66
%72 = load i32, i32* %8
%73 = add nsw i32 %72, 1
store i32 %73, i32* %8
br label %45
74: ; preds = %45
%75 = call i32 @regfree(i32* %6)
%76 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [12 x i8] c"no pattern\0A\00"
@stderr = global i32 0
@REG_EXTENDED = global i32 0
@REG_NOSUB = global i32 0
@REG_NEWLINE = global i32 0
@stdin = global i32* null
@.str.1 = [2 x i8] c"r\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [4096 x i8]
%5 = bitcast i32* %3 to i8*
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @REG_EXTENDED
%15 = load i32, i32* @REG_NOSUB
%16 = or i32 %15, %14
%17 = load i32, i32* @REG_NEWLINE
%18 = or i32 %16, %17
%19 = call i32 @regcomp(i32* %3, i8* %13, i32 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %11
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%23 = call i32 @regerror(i32 %19, i32* %3, i8* %22, i32 4096)
%24 = call i32 @puts(i8* %22)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %11
%27 = icmp eq i32 %0, 2
br i1 %27, label %30, label %28
28: ; preds = %26
%29 = zext i32 %0 to i64
br label %35
30: ; preds = %26
%31 = load i32*, i32** @stdin
%32 = call i32 @do_grep(i32* %3, i32* %31)
%33 = call i32 @regfree(i32* %3)
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %28, %47
%36 = phi i64 [ 2, %28 ], [ %50, %47 ]
%37 = getelementptr inbounds i8*, i8** %1, i64 %36
%38 = load i8*, i8** %37
%39 = call i32* @fopen(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %47
41: ; preds = %35
%42 = and i64 %36, 4294967295
%43 = getelementptr inbounds i8*, i8** %1, i64 %42
%44 = load i8*, i8** %43
%45 = call i32 @perror(i8* %44)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %35
%48 = call i32 @do_grep(i32* %3, i32* %39)
%49 = call i32 @fclose(i32* %39)
%50 = add nuw nsw i64 %36, 1
%51 = icmp eq i64 %50, %29
br i1 %51, label %52, label %35
52: ; preds = %47
%53 = call i32 @regfree(i32* %3)
%54 = call i32 @exit(i32 0)
unreachable
}