#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 */
struct Program {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int BUFSIZ;
scalar_t__ EOF;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int print(char *, char **, char *, char *);
int readline(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ strcmp(char *, char *);
int vm(struct Program *, char *, char **);
int grep(struct Program *prog, char *infile, char *outfmt) {
  char buf[BUFSIZ], *captures[20];
  int rc, matched = 0;
  FILE *fin = stdin;
  if (infile && !strcmp(infile, "-"))
    infile = "(standard input)";
  else if (infile && (fin = fopen(infile, "r")) == NULL) {
    perror(infile);
    return -1;
  }
  while ((rc = readline(buf, sizeof buf, fin)) > 0) {
    if ((rc = vm(prog, buf, captures)) > 0) {
      matched = 1;
      rc = print(buf, captures, infile, outfmt);
    }
    if (rc < 0)
      break;
  }
  if (rc < 0) {
    perror(infile ? infile : "(standard input)");
    matched = -1;
  }
  if (fin && fin != stdin && fclose(fin) == EOF) {
    perror(infile);
    return -1;
  }
  return matched;
}


x86 assembly (clang, -O0):
<grep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0xf8(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xf0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x0,-0xd8(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0xe0(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     120c <grep+0x9c>
mov    -0x18(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    120c <grep+0x9c>
lea    0xdff(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    125c <grep+0xec>
cmpq   $0x0,-0x18(%rbp)
je     1257 <grep+0xe7>
mov    -0x18(%rbp),%rdi
lea    0xdf1(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0xe0(%rbp)
cmp    $0x0,%rax
jne    1257 <grep+0xe7>
mov    -0x18(%rbp),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0xe4(%rbp)
jmp    13b8 <grep+0x248>
jmp    125c <grep+0xec>
jmp    1261 <grep+0xf1>
mov    -0xf0(%rbp),%rdi
mov    -0xf8(%rbp),%rax
mov    %eax,%esi
mov    -0xe0(%rbp),%rdx
call   1040 <readline@plt>
mov    %eax,-0xd4(%rbp)
cmp    $0x0,%eax
jle    12f4 <grep+0x184>
mov    -0xf0(%rbp),%rsi
mov    -0x10(%rbp),%rdi
lea    -0xd0(%rbp),%rdx
call   1070 <vm@plt>
mov    %eax,-0xd4(%rbp)
cmp    $0x0,%eax
jle    12dd <grep+0x16d>
mov    -0xf0(%rbp),%rdi
movl   $0x1,-0xd8(%rbp)
lea    -0xd0(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1050 <print@plt>
mov    %eax,-0xd4(%rbp)
cmpl   $0x0,-0xd4(%rbp)
jge    12ef <grep+0x17f>
jmp    12f4 <grep+0x184>
jmp    1261 <grep+0xf1>
cmpl   $0x0,-0xd4(%rbp)
jge    1345 <grep+0x1d5>
cmpq   $0x0,-0x18(%rbp)
je     131c <grep+0x1ac>
mov    -0x18(%rbp),%rax
mov    %rax,-0x100(%rbp)
jmp    132f <grep+0x1bf>
lea    0xcdf(%rip),%rax
mov    %rax,-0x100(%rbp)
jmp    132f <grep+0x1bf>
mov    -0x100(%rbp),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0xd8(%rbp)
ff ff ff
cmpq   $0x0,-0xe0(%rbp)
je     13a5 <grep+0x235>
mov    -0xe0(%rbp),%rax
mov    0x2c87(%rip),%rcx
cmp    (%rcx),%rax
je     13a5 <grep+0x235>
mov    -0xe0(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2c63(%rip),%rcx
cmp    (%rcx),%rax
jne    13a5 <grep+0x235>
mov    -0x18(%rbp),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0xe4(%rbp)
jmp    13b8 <grep+0x248>
mov    -0xd8(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0xe4(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grep>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb8,%rsp
mov    %rdx,-0x40(%rbp)
mov    %rdi,%r13
mov    0x2e3e(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r15
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    0x2e3b(%rip),%rax
mov    (%rax),%rbx
test   %rsi,%rsi
je     11f2 <grep+0x82>
mov    %rsi,%r12
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
lea    0xe3b(%rip),%rsi
mov    %r12,%rdi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11fc <grep+0x8c>
lea    0xe3b(%rip),%rsi
mov    %r12,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     12bf <grep+0x14f>
mov    %rax,%rbx
mov    %r12,-0x38(%rbp)
jmp    1207 <grep+0x97>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
jmp    1203 <grep+0x93>
lea    0xdff(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    -0xe0(%rbp),%r12
jmp    1214 <grep+0xa4>
test   %eax,%eax
js     1287 <grep+0x117>
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
call   1040 <readline@plt>
test   %eax,%eax
jle    1256 <grep+0xe6>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1070 <vm@plt>
test   %eax,%eax
jle    1210 <grep+0xa0>
mov    %r15,%rdi
mov    %r12,%rsi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rcx
call   1050 <print@plt>
mov    $0x1,%ecx
mov    %rcx,-0x30(%rbp)
jmp    1210 <grep+0xa0>
js     1287 <grep+0x117>
test   %rbx,%rbx
je     12ac <grep+0x13c>
mov    0x2d84(%rip),%rax
cmp    (%rax),%rbx
je     12ac <grep+0x13c>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2d68(%rip),%rcx
cmp    (%rcx),%rax
mov    -0x30(%rbp),%rax
jne    12b0 <grep+0x140>
mov    -0x38(%rbp),%rdi
jmp    12c2 <grep+0x152>
mov    -0x38(%rbp),%rax
test   %rax,%rax
lea    0xd6d(%rip),%rdi
cmovne %rax,%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
mov    %rax,-0x30(%rbp)
test   %rbx,%rbx
jne    125d <grep+0xed>
mov    -0x30(%rbp),%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    12b0 <grep+0x140>

LLVM IR (-O0):
%struct.Program = type { i32 }
@BUFSIZ = global i32 0
@stdin = global i32* null
@.str = [2 x i8] c"-\00"
@.str.1 = [17 x i8] c"(standard input)\00"
@.str.2 = [2 x i8] c"r\00"
@EOF = global i64 0
define i32 @grep(%struct.Program* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.Program*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca [20 x i8*]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i32
store %struct.Program* %0, %struct.Program** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%15 = load i32, i32* @BUFSIZ
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %8
%18 = alloca i8, i64 %16
store i64 %16, i64* %9
store i32 0, i32* %12
%19 = load i32*, i32** @stdin
store i32* %19, i32** %13
%20 = load i8*, i8** %6
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %27
22: ; preds = %3
%23 = load i8*, i8** %6
%24 = call i32 @strcmp(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %22
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8** %6
br label %38
27: ; preds = %22, %3
%28 = load i8*, i8** %6
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %37
30: ; preds = %27
%31 = load i8*, i8** %6
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %32, i32** %13
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i8*, i8** %6
%36 = call i32 @perror(i8* %35)
store i32 -1, i32* %4
store i32 1, i32* %14
br label %88
37: ; preds = %30, %27
br label %38
38: ; preds = %37, %26
br label %39
39: ; preds = %58, %38
%40 = trunc i64 %16 to i32
%41 = load i32*, i32** %13
%42 = call i32 @readline(i8* %18, i32 %40, i32* %41)
store i32 %42, i32* %11
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %59
44: ; preds = %39
%45 = load %struct.Program*, %struct.Program** %5
%46 = getelementptr inbounds [20 x i8*], [20 x i8*]* %10, i64 0, i64 0
%47 = call i32 @vm(%struct.Program* %45, i8* %18, i8** %46)
store i32 %47, i32* %11
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %44
store i32 1, i32* %12
%50 = getelementptr inbounds [20 x i8*], [20 x i8*]* %10, i64 0, i64 0
%51 = load i8*, i8** %6
%52 = load i8*, i8** %7
%53 = call i32 @print(i8* %18, i8** %50, i8* %51, i8* %52)
store i32 %53, i32* %11
br label %54
54: ; preds = %49, %44
%55 = load i32, i32* %11
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %54
br label %59
58: ; preds = %54
br label %39
59: ; preds = %57, %39
%60 = load i32, i32* %11
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %71
62: ; preds = %59
%63 = load i8*, i8** %6
%64 = icmp ne i8* %63, null
br i1 %64, label %65, label %67
65: ; preds = %62
%66 = load i8*, i8** %6
br label %68
67: ; preds = %62
br label %68
68: ; preds = %67, %65
%69 = phi i8* [ %66, %65 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %67 ]
%70 = call i32 @perror(i8* %69)
store i32 -1, i32* %12
br label %71
71: ; preds = %68, %59
%72 = load i32*, i32** %13
%73 = icmp ne i32* %72, null
br i1 %73, label %74, label %86
74: ; preds = %71
%75 = load i32*, i32** %13
%76 = load i32*, i32** @stdin
%77 = icmp ne i32* %75, %76
br i1 %77, label %78, label %86
78: ; preds = %74
%79 = load i32*, i32** %13
%80 = call i64 @fclose(i32* %79)
%81 = load i64, i64* @EOF
%82 = icmp eq i64 %80, %81
br i1 %82, label %83, label %86
83: ; preds = %78
%84 = load i8*, i8** %6
%85 = call i32 @perror(i8* %84)
store i32 -1, i32* %4
store i32 1, i32* %14
br label %88
86: ; preds = %78, %74, %71
%87 = load i32, i32* %12
store i32 %87, i32* %4
store i32 1, i32* %14
br label %88
88: ; preds = %86, %83, %34
%89 = load i8*, i8** %8
%90 = load i32, i32* %4
ret i32 %90
}

LLVM IR (-O2):
%struct.Program = type { i32 }
@BUFSIZ = global i32 0
@stdin = global i32* null
@.str = [2 x i8] c"-\00"
@.str.1 = [17 x i8] c"(standard input)\00"
@.str.2 = [2 x i8] c"r\00"
@EOF = global i64 0
define i32 @grep(%struct.Program* %0, i8* %1, i8* %2) {
%4 = alloca [20 x i8*]
%5 = load i32, i32* @BUFSIZ
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast [20 x i8*]* %4 to i8*
%9 = load i32*, i32** @stdin
%10 = icmp eq i8* %1, null
br i1 %10, label %19, label %11
11: ; preds = %3
%12 = tail call i32 @strcmp(i8* %1, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @perror(i8* %1)
br label %54
19: ; preds = %3, %11, %14
%20 = phi i8* [ %1, %14 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %11 ], [ null, %3 ]
%21 = phi i32* [ %15, %14 ], [ %9, %11 ], [ %9, %3 ]
%22 = getelementptr inbounds [20 x i8*], [20 x i8*]* %4, i64 0, i64 0
br label %23
23: ; preds = %32, %19
%24 = phi i32 [ 0, %19 ], [ %34, %32 ]
%25 = call i32 @readline(i8* %7, i32 %5, i32* %21)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %23
%28 = call i32 @vm(%struct.Program* %0, i8* %7, i8** %22)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @print(i8* %7, i8** %22, i8* %20, i8* %2)
br label %32
32: ; preds = %30, %27
%33 = phi i32 [ %31, %30 ], [ %28, %27 ]
%34 = phi i32 [ 1, %30 ], [ %24, %27 ]
%35 = icmp slt i32 %33, 0
br i1 %35, label %38, label %23
36: ; preds = %23
%37 = icmp slt i32 %25, 0
br i1 %37, label %38, label %42
38: ; preds = %32, %36
%39 = icmp eq i8* %20, null
%40 = select i1 %39, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %20
%41 = call i32 @perror(i8* %40)
br label %42
42: ; preds = %38, %36
%43 = phi i32 [ -1, %38 ], [ %24, %36 ]
%44 = icmp eq i32* %21, null
%45 = load i32*, i32** @stdin
%46 = icmp eq i32* %21, %45
%47 = select i1 %44, i1 true, i1 %46
br i1 %47, label %54, label %48
48: ; preds = %42
%49 = call i64 @fclose(i32* %21)
%50 = load i64, i64* @EOF
%51 = icmp eq i64 %49, %50
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @perror(i8* %20)
br label %54
54: ; preds = %42, %48, %52, %17
%55 = phi i32 [ -1, %17 ], [ -1, %52 ], [ %43, %48 ], [ %43, %42 ]
ret i32 %55
}