#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 /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ grep(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int fd, i;
  char *pattern;
  if (argc <= 1) {
    fprintf(stderr, "usage: grep pattern [file ...]\n");
    exit(0);
  }
  pattern = argv[1];
  if (argc <= 2) {
    grep(pattern, 0);
    exit(0);
  }
  for (i = 2; i < argc; i++) {
    if ((fd = open(argv[i], 0)) < 0) {
      fprintf(stdout, "grep: cannot open %s\n", argv[i]);
      exit(0);
    }
    grep(pattern, fd);
    close(fd);
  }
  return 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     118e <main+0x3e>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jg     11b6 <main+0x66>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1040 <grep@plt>
xor    %edi,%edi
call   1070 <exit@plt>
movl   $0x2,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1234 <main+0xe4>
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    1212 <main+0xc2>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <grep@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11bd <main+0x6d>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x1,%edi
jle    11da <main+0x8a>
mov    %rsi,%r14
mov    0x8(%rsi),%r15
cmp    $0x2,%edi
je     11f8 <main+0xa8>
mov    %edi,%r12d
mov    $0x2,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r14,%rbx,8),%rdi
xor    %esi,%esi
call   1060 <open@plt>
test   %eax,%eax
js     11b6 <main+0x66>
mov    %eax,%ebp
mov    %r15,%rdi
mov    %eax,%esi
call   1040 <grep@plt>
mov    %ebp,%edi
call   1030 <close@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1180 <main+0x30>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%eax
mov    0x2e19(%rip),%rcx
mov    (%rcx),%edi
mov    (%r14,%rax,8),%rdx
lea    0xe54(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <grep@plt>
xor    %edi,%edi
call   1070 <exit@plt>

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

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