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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ grep(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int fd, i;
  char *pattern;
  if (argc <= 1) {
    printf("usage: grep pattern [file ...]\n");
    exit(-1);
  }
  pattern = argv[1];
  if (argc <= 2) {
    grep(pattern, 0);
    exit(0);
  }
  for (i = 2; i < argc; i++) {
    if ((fd = open(argv[i], 0)) < 0) {
      printf("grep: cannot open %s\n", argv[i]);
      exit(-1);
    }
    grep(pattern, fd);
    close(fd);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     1188 <main+0x38>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jg     11b0 <main+0x60>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1050 <grep@plt>
xor    %edi,%edi
call   1070 <exit@plt>
movl   $0x2,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1228 <main+0xd8>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1206 <main+0xb6>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <grep@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11b7 <main+0x67>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x1,%edi
jg     1183 <main+0x23>
lea    0xea2(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    %rsi,%r14
mov    0x8(%rsi),%r15
cmp    $0x2,%edi
jne    11a0 <main+0x40>
mov    %r15,%rdi
xor    %esi,%esi
call   1060 <grep@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    %edi,%r12d
mov    $0x2,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%r14,%rbx,8),%rdi
xor    %esi,%esi
call   1070 <open@plt>
test   %eax,%eax
js     11e2 <main+0x82>
mov    %eax,%ebp
mov    %r15,%rdi
mov    %eax,%esi
call   1060 <grep@plt>
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11b0 <main+0x50>
xor    %edi,%edi
call   1080 <exit@plt>
mov    %ebx,%eax
mov    (%r14,%rax,8),%rsi
lea    0xe11(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"usage: grep pattern [file ...]\0A\00"
@.str.1 = [22 x i8] c"grep: cannot open %s\0A\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 i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp sle i32 %9, 1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %8
%18 = load i32, i32* %4
%19 = icmp sle i32 %18, 2
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i8*, i8** %8
%22 = call i32 @grep(i8* %21, i32 0)
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %14
store i32 2, i32* %7
br label %25
25: ; preds = %51, %24
%26 = load i32, i32* %7
%27 = load i32, i32* %4
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %54
29: ; preds = %25
%30 = load i8**, i8*** %5
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8*, i8** %30, i64 %32
%34 = load i8*, i8** %33
%35 = call i32 @open(i8* %34, i32 0)
store i32 %35, i32* %6
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %45
37: ; preds = %29
%38 = load i8**, i8*** %5
%39 = load i32, i32* %7
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %38, i64 %40
%42 = load i8*, i8** %41
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %42)
%44 = call i32 @exit(i32 -1)
unreachable
45: ; preds = %29
%46 = load i8*, i8** %8
%47 = load i32, i32* %6
%48 = call i32 @grep(i8* %46, i32 %47)
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %45
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 1
store i32 %53, i32* %7
br label %25
54: ; preds = %25
%55 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [22 x i8] c"grep: cannot open %s\0A\00"
@str = [31 x i8] c"usage: grep pattern [file ...]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = icmp eq i32 %0, 2
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = zext i32 %0 to i64
br label %16
13: ; preds = %7
%14 = tail call i32 @grep(i8* %9, i32 0)
%15 = tail call i32 @exit(i32 0)
unreachable
16: ; preds = %11, %28
%17 = phi i64 [ 2, %11 ], [ %31, %28 ]
%18 = getelementptr inbounds i8*, i8** %1, i64 %17
%19 = load i8*, i8** %18
%20 = tail call i32 @open(i8* %19, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %16
%23 = and i64 %17, 4294967295
%24 = getelementptr inbounds i8*, i8** %1, i64 %23
%25 = load i8*, i8** %24
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = tail call i32 @exit(i32 -1)
unreachable
28: ; preds = %16
%29 = tail call i32 @grep(i8* %9, i32 %20)
%30 = tail call i32 @close(i32 %20)
%31 = add nuw nsw i64 %17, 1
%32 = icmp eq i64 %31, %12
br i1 %32, label %33, label %16
33: ; preds = %28
%34 = tail call i32 @exit(i32 0)
unreachable
}