#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[1024];
    regerror(err, &pat, buf, sizeof buf);
    puts(buf);
    exit(1);
  }
  if (argc == 2) {
    do_grep(&pat, stdin);
  } else {
    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    $0x430,%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    -0x420(%rbp),%rdx
lea    -0x14(%rbp),%rsi
mov    $0x400,%ecx
call   1080 <regerror@plt>
lea    -0x420(%rbp),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jne    126b <main+0xcb>
mov    0x2d8e(%rip),%rax
mov    (%rax),%rsi
lea    -0x14(%rbp),%rdi
call   1090 <do_grep@plt>
jmp    12f5 <main+0x155>
movl   $0x2,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12f0 <main+0x150>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xd7b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    12c6 <main+0x126>
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    -0x428(%rbp),%rsi
lea    -0x14(%rbp),%rdi
call   1090 <do_grep@plt>
mov    -0x428(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1272 <main+0xd2>
jmp    12f5 <main+0x155>
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    $0x418,%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    1222 <main+0x82>
cmp    $0x2,%ebp
jne    124d <main+0xad>
mov    0x2dd5(%rip),%rax
mov    (%rax),%rsi
lea    0xc(%rsp),%rdi
call   1090 <do_grep@plt>
jmp    12a0 <main+0x100>
lea    0xc(%rsp),%rsi
lea    0x10(%rsp),%rbx
mov    %eax,%edi
mov    %rbx,%rdx
mov    $0x400,%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    0xdb0(%rip),%r14
lea    0xc(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   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 [1024 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 [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%32 = call i32 @regerror(i32 %30, i32* %6, i8* %31, i32 1024)
%33 = getelementptr inbounds [1024 x i8], [1024 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 %42
39: ; preds = %36
%40 = load i32*, i32** @stdin
%41 = call i32 @do_grep(i32* %6, i32* %40)
br label %73
42: ; preds = %36
store i32 2, i32* %8
br label %43
43: ; preds = %69, %42
%44 = load i32, i32* %8
%45 = load i32, i32* %4
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %72
47: ; preds = %43
%48 = load i8**, i8*** %5
%49 = load i32, i32* %8
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8*, i8** %48, i64 %50
%52 = load i8*, i8** %51
%53 = call i32* @fopen(i8* %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %53, i32** %10
%54 = load i32*, i32** %10
%55 = icmp ne i32* %54, null
br i1 %55, label %64, label %56
56: ; preds = %47
%57 = load i8**, i8*** %5
%58 = load i32, i32* %8
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8*, i8** %57, i64 %59
%61 = load i8*, i8** %60
%62 = call i32 @perror(i8* %61)
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %47
%65 = load i32*, i32** %10
%66 = call i32 @do_grep(i32* %6, i32* %65)
%67 = load i32*, i32** %10
%68 = call i32 @fclose(i32* %67)
br label %69
69: ; preds = %64
%70 = load i32, i32* %8
%71 = add nsw i32 %70, 1
store i32 %71, i32* %8
br label %43
72: ; preds = %43
br label %73
73: ; preds = %72, %39
%74 = call i32 @regfree(i32* %6)
%75 = 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 [1024 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 [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%23 = call i32 @regerror(i32 %19, i32* %3, i8* %22, i32 1024)
%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 %33
30: ; preds = %26
%31 = load i32*, i32** @stdin
%32 = call i32 @do_grep(i32* %3, i32* %31)
br label %50
33: ; preds = %28, %45
%34 = phi i64 [ 2, %28 ], [ %48, %45 ]
%35 = getelementptr inbounds i8*, i8** %1, i64 %34
%36 = load i8*, i8** %35
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %45
39: ; preds = %33
%40 = and i64 %34, 4294967295
%41 = getelementptr inbounds i8*, i8** %1, i64 %40
%42 = load i8*, i8** %41
%43 = call i32 @perror(i8* %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %33
%46 = call i32 @do_grep(i32* %3, i32* %37)
%47 = call i32 @fclose(i32* %37)
%48 = add nuw nsw i64 %34, 1
%49 = icmp eq i64 %48, %29
br i1 %49, label %50, label %33
50: ; preds = %45, %30
%51 = call i32 @regfree(i32* %3)
%52 = call i32 @exit(i32 0)
unreachable
}