#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*/ exit(int);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
size_t mbstowcs(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
scalar_t__ wcsstr(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
void do_grep(wchar_t *pattern, FILE *src) {
  char buf[4096];
  wchar_t wbuf[4096];
  while (fgets(buf, sizeof buf, src)) {
    if (mbstowcs(wbuf, buf, 4096) == (size_t)(-1)) {
      fprintf(stderr, "mbstowcs failed #2\n");
      exit(1);
    }
    if (wcsstr(wbuf, pattern)) {
      fputs(buf, stdout);
    }
  }
}


x86 assembly (clang, -O0):
<do_grep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x5010,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1000,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1207 <do_grep+0xa7>
lea    -0x5010(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <mbstowcs@plt>
cmp    $0xffffffffffffffff,%rax
jne    11d3 <do_grep+0x73>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x5010(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1070 <wcsstr@plt>
cmp    $0x0,%rax
je     1202 <do_grep+0xa2>
lea    -0x1010(%rbp),%rdi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
jmp    1173 <do_grep+0x13>
add    $0x5010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_grep>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x5000,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11e9 <do_grep+0x89>
lea    0x1000(%rsp),%r12
mov    %rsp,%rbx
mov    0x2e3b(%rip),%r13
jmp    11b5 <do_grep+0x55>
nop
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11e9 <do_grep+0x89>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1030 <mbstowcs@plt>
cmp    $0xffffffffffffffff,%rax
je     11fa <do_grep+0x9a>
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <wcsstr@plt>
test   %rax,%rax
je     11a0 <do_grep+0x40>
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1040 <fputs@plt>
jmp    11a0 <do_grep+0x40>
add    $0x5000,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"mbstowcs failed\0A\00"
@stdout = global i32 0
define void @do_grep(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca [4096 x i8]
%6 = alloca [4096 x i32]
store i32* %0, i32** %3
store i32* %1, i32** %4
br label %7
7: ; preds = %30, %2
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%9 = load i32*, i32** %4
%10 = call i64 @fgets(i8* %8, i32 4096, i32* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %31
12: ; preds = %7
%13 = getelementptr inbounds [4096 x i32], [4096 x i32]* %6, i64 0, i64 0
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%15 = call i64 @mbstowcs(i32* %13, i8* %14, i32 4096)
%16 = icmp eq i64 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = getelementptr inbounds [4096 x i32], [4096 x i32]* %6, i64 0, i64 0
%23 = load i32*, i32** %3
%24 = call i64 @wcsstr(i32* %22, i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%28 = load i32, i32* @stdout
%29 = call i32 @fputs(i8* %27, i32 %28)
br label %30
30: ; preds = %26, %21
br label %7
31: ; preds = %7
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"mbstowcs failed\0A\00"
@stdout = global i32 0
define void @do_grep(i32* %0, i32* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = alloca [4096 x i32]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = bitcast [4096 x i32]* %4 to i8*
%7 = call i64 @fgets(i8* %5, i32 4096, i32* %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %27, label %9
9: ; preds = %2
%10 = getelementptr inbounds [4096 x i32], [4096 x i32]* %4, i64 0, i64 0
br label %11
11: ; preds = %9, %24
%12 = call i64 @mbstowcs(i32* %10, i8* %5, i32 4096)
%13 = icmp eq i64 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = call i64 @wcsstr(i32* %10, i32* %0)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i32, i32* @stdout
%23 = call i32 @fputs(i8* %5, i32 %22)
br label %24
24: ; preds = %21, %18
%25 = call i64 @fgets(i8* %5, i32 4096, i32* %1)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %11
27: ; preds = %24, %2
ret void
}