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

/* Variables and functions */
int /*<<< orphan*/ LC_CTYPE;
int /*<<< orphan*/ do_grep(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
size_t mbstowcs(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setlocale(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
int main(int argc, char *argv[]) {
  wchar_t pattern[1024];
  int i;
  setlocale(LC_CTYPE, "");
  if (argc < 2) {
    fputs("no pattern\n", stderr);
    exit(1);
  }
  if (mbstowcs(pattern, argv[1], 1024) == (size_t)(-1)) {
    fprintf(stderr, "mbstowcs failed #1\n");
    exit(1);
  }
  if (argc == 2) {
    do_grep(pattern, stdin);
  } else {
    for (i = 2; i < argc; i++) {
      FILE *f;
      f = fopen(argv[i], "r");
      if (!f) {
        perror(argv[i]);
        exit(1);
      }
      do_grep(pattern, f);
      fclose(f);
    }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1080 <setlocale@plt>
cmpl   $0x2,-0x8(%rbp)
jge    11e7 <main+0x57>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
lea    0xe28(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x400,%edx
call   1030 <mbstowcs@plt>
cmp    $0xffffffffffffffff,%rax
jne    1229 <main+0x99>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jne    124e <main+0xbe>
lea    -0x1010(%rbp),%rdi
mov    0x2d9f(%rip),%rax
mov    (%rax),%rsi
call   1070 <do_grep@plt>
jmp    12ed <main+0x15d>
movl   $0x2,-0x1014(%rbp)
mov    -0x1014(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12e8 <main+0x158>
mov    -0x10(%rbp),%rax
movslq -0x1014(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xda3(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x1020(%rbp)
cmpq   $0x0,-0x1020(%rbp)
jne    12b5 <main+0x125>
mov    -0x10(%rbp),%rax
movslq -0x1014(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x1020(%rbp),%rsi
call   1070 <do_grep@plt>
mov    -0x1020(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1014(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1014(%rbp)
jmp    1258 <main+0xc8>
jmp    12ed <main+0x15d>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%r12
mov    %edi,%ebp
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0xe69(%rip),%rsi
call   1080 <setlocale@plt>
cmp    $0x1,%ebp
jg     11df <main+0x4f>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0xe30(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x8(%r12),%rsi
mov    %rsp,%rdi
mov    $0x400,%edx
call   1030 <mbstowcs@plt>
cmp    $0xffffffffffffffff,%rax
jne    1216 <main+0x86>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmp    $0x2,%ebp
jne    1234 <main+0xa4>
mov    0x2dbe(%rip),%rax
mov    (%rax),%rsi
mov    %rsp,%rdi
call   1070 <do_grep@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    %ebp,%r13d
mov    $0x2,%ebx
lea    0xddd(%rip),%r14
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
mov    (%r12,%rbx,8),%rdi
mov    %r14,%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1287 <main+0xf7>
mov    %rax,%rbp
mov    %r15,%rdi
mov    %rax,%rsi
call   1070 <do_grep@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1250 <main+0xc0>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    %ebx,%eax
mov    (%r12,%rax,8),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@LC_CTYPE = global i32 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [12 x i8] c"no pattern\0A\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"mbstowcs failed\0A\00"
@stdin = global i32* null
@.str.3 = [2 x i8] c"r\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i32]
%7 = alloca i32
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @LC_CTYPE
%10 = call i32 @setlocale(i32 %9, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%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.1, i64 0, i64 0), i32 %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = getelementptr inbounds [1024 x i32], [1024 x i32]* %6, i64 0, i64 0
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @mbstowcs(i32* %18, i8* %21, i32 1024)
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %17
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, 2
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = getelementptr inbounds [1024 x i32], [1024 x i32]* %6, i64 0, i64 0
%33 = load i32*, i32** @stdin
%34 = call i32 @do_grep(i32* %32, i32* %33)
br label %67
35: ; preds = %28
store i32 2, i32* %7
br label %36
36: ; preds = %63, %35
%37 = load i32, i32* %7
%38 = load i32, i32* %4
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %66
40: ; preds = %36
%41 = load i8**, i8*** %5
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8*, i8** %41, i64 %43
%45 = load i8*, i8** %44
%46 = call i32* @fopen(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %46, i32** %8
%47 = load i32*, i32** %8
%48 = icmp ne i32* %47, null
br i1 %48, label %57, label %49
49: ; preds = %40
%50 = load i8**, i8*** %5
%51 = load i32, i32* %7
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8*, i8** %50, i64 %52
%54 = load i8*, i8** %53
%55 = call i32 @perror(i8* %54)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %40
%58 = getelementptr inbounds [1024 x i32], [1024 x i32]* %6, i64 0, i64 0
%59 = load i32*, i32** %8
%60 = call i32 @do_grep(i32* %58, i32* %59)
%61 = load i32*, i32** %8
%62 = call i32 @fclose(i32* %61)
br label %63
63: ; preds = %57
%64 = load i32, i32* %7
%65 = add nsw i32 %64, 1
store i32 %65, i32* %7
br label %36
66: ; preds = %36
br label %67
67: ; preds = %66, %31
%68 = call i32 @exit(i32 0)
unreachable
}

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