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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char const *, int, size_t, int /*<<< orphan*/);
int getchar();
int /*<<< orphan*/ putchar(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ usage(char *);
int main(int argc, char *argv[]) {
  int c;
  size_t n;
  FILE *f;
  char buffer[512];
  char const *pattern = argv[1];
  char const *p = pattern;
  if (argc != 3)
    usage(argv[0]);
  f = fopen(argv[2], "r");
  if (!f) {
    fprintf(stderr, "Failed to open file: %s\n", argv[2]);
    return 1;
  }
  while (c = getchar(), c != EOF) {
    if (c != *p) {
      fwrite(pattern, 1, p - pattern, stdout);
      p = pattern;
      putchar(c);
      continue;
    }
    if (!*++p) {
      rewind(f);
      p = pattern;
      while (n = fread(buffer, 1, 512, f))
        fwrite(buffer, 1, n, stdout);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x238(%rbp)
mov    -0x238(%rbp),%rax
mov    %rax,-0x240(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11cc <main+0x4c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe25(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1218 <main+0x98>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xdfb(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1326 <main+0x1a6>
jmp    121d <main+0x9d>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2db7(%rip),%rcx
cmp    (%rcx),%eax
je     131f <main+0x19f>
mov    -0x14(%rbp),%eax
mov    -0x240(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1294 <main+0x114>
mov    -0x238(%rbp),%rdi
mov    -0x240(%rbp),%rdx
mov    -0x238(%rbp),%rax
sub    %rax,%rdx
mov    0x2d63(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
mov    -0x238(%rbp),%rax
mov    %rax,-0x240(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <putchar@plt>
jmp    121d <main+0x9d>
mov    -0x240(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x240(%rbp)
cmpb   $0x0,0x1(%rax)
jne    131a <main+0x19a>
mov    -0x28(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x238(%rbp),%rax
mov    %rax,-0x240(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1040 <fread@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1315 <main+0x195>
lea    -0x230(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    0x2ccc(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
jmp    12ca <main+0x14a>
jmp    131a <main+0x19a>
jmp    121d <main+0x9d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rax
mov    %rax,0x8(%rsp)
cmp    $0x3,%edi
je     11aa <main+0x2a>
mov    (%rbx),%rdi
call   1080 <usage@plt>
mov    0x10(%rbx),%rdi
lea    0xe4b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12b7 <main+0x137>
mov    %rax,%r15
xor    %r13d,%r13d
xor    %eax,%eax
call   1060 <getchar@plt>
mov    %eax,%ebp
mov    0x2e0f(%rip),%rax
cmp    (%rax),%ebp
je     12d6 <main+0x156>
lea    0x10(%rsp),%r12
mov    0x2de3(%rip),%r14
mov    0x8(%rsp),%rbx
jmp    1240 <main+0xc0>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x8(%rsp),%r13
sub    %r13,%rbx
mov    (%r14),%ecx
mov    %r13,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
call   10a0 <fwrite@plt>
mov    %ebp,%edi
call   1030 <putchar@plt>
mov    %r13,%rbx
xor    %r13d,%r13d
xor    %eax,%eax
call   1060 <getchar@plt>
mov    %eax,%ebp
mov    0x2db0(%rip),%rax
cmp    (%rax),%ebp
je     12d6 <main+0x156>
movsbl (%rbx),%eax
cmp    %eax,%ebp
jne    1200 <main+0x80>
cmpb   $0x0,0x1(%rbx)
je     1253 <main+0xd3>
add    $0x1,%rbx
jmp    1225 <main+0xa5>
mov    %r15,%rdi
call   1050 <rewind@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    0x8(%rsp),%rbx
test   %rax,%rax
je     1225 <main+0xa5>
nopw   0x0(%rax,%rax,1)
mov    (%r14),%ecx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
call   10a0 <fwrite@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
jne    1280 <main+0x100>
mov    0x8(%rsp),%rbx
jmp    1225 <main+0xa5>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rbx),%rdx
lea    0xd37(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%r13d
mov    %r13d,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Failed to open file: %s\0A\00"
@EOF = global i32 0
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32*
%9 = alloca [512 x i8]
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
store i8* %14, i8** %10
%15 = load i8*, i8** %10
store i8* %15, i8** %11
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 3
br i1 %17, label %18, label %23
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 @usage(i8* %21)
br label %23
23: ; preds = %18, %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %27, i32** %8
%28 = load i32*, i32** %8
%29 = icmp ne i32* %28, null
br i1 %29, label %36, label %30
30: ; preds = %23
%31 = load i32, i32* @stderr
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %34)
store i32 1, i32* %3
br label %82
36: ; preds = %23
br label %37
37: ; preds = %80, %48, %36
%38 = call i32 (...) @getchar()
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = load i32, i32* @EOF
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %81
42: ; preds = %37
%43 = load i32, i32* %6
%44 = load i8*, i8** %11
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp ne i32 %43, %46
br i1 %47, label %48, label %60
48: ; preds = %42
%49 = load i8*, i8** %10
%50 = load i8*, i8** %11
%51 = load i8*, i8** %10
%52 = ptrtoint i8* %50 to i64
%53 = ptrtoint i8* %51 to i64
%54 = sub i64 %52, %53
%55 = load i32, i32* @stdout
%56 = call i32 @fwrite(i8* %49, i32 1, i64 %54, i32 %55)
%57 = load i8*, i8** %10
store i8* %57, i8** %11
%58 = load i32, i32* %6
%59 = call i32 @putchar(i32 %58)
br label %37
60: ; preds = %42
%61 = load i8*, i8** %11
%62 = getelementptr inbounds i8, i8* %61, i32 1
store i8* %62, i8** %11
%63 = load i8, i8* %62
%64 = icmp ne i8 %63, 0
br i1 %64, label %80, label %65
65: ; preds = %60
%66 = load i32*, i32** %8
%67 = call i32 @rewind(i32* %66)
%68 = load i8*, i8** %10
store i8* %68, i8** %11
br label %69
69: ; preds = %74, %65
%70 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%71 = load i32*, i32** %8
%72 = call i64 @fread(i8* %70, i32 1, i32 512, i32* %71)
store i64 %72, i64* %7
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %79
74: ; preds = %69
%75 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%76 = load i64, i64* %7
%77 = load i32, i32* @stdout
%78 = call i32 @fwrite(i8* %75, i32 1, i64 %76, i32 %77)
br label %69
79: ; preds = %69
br label %80
80: ; preds = %79, %60
br label %37
81: ; preds = %37
store i32 0, i32* %3
br label %82
82: ; preds = %81, %30
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Failed to open file: %s\0A\00"
@EOF = global i32 0
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = icmp eq i32 %0, 3
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @usage(i8* %9)
br label %11
11: ; preds = %8, %2
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %22, label %16
16: ; preds = %11
%17 = call i32 (...) @getchar()
%18 = load i32, i32* @EOF
%19 = icmp eq i32 %17, %18
br i1 %19, label %57, label %20
20: ; preds = %16
%21 = ptrtoint i8* %6 to i64
br label %26
22: ; preds = %11
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %12
%25 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %57
26: ; preds = %20, %38
%27 = phi i32 [ %17, %20 ], [ %40, %38 ]
%28 = phi i8* [ %6, %20 ], [ %39, %38 ]
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %27, %30
br i1 %31, label %43, label %32
32: ; preds = %26
%33 = ptrtoint i8* %28 to i64
%34 = sub i64 %33, %21
%35 = load i32, i32* @stdout
%36 = call i32 @fwrite(i8* %6, i32 1, i64 %34, i32 %35)
%37 = call i32 @putchar(i32 %27)
br label %38
38: ; preds = %51, %43, %47, %32
%39 = phi i8* [ %6, %32 ], [ %44, %43 ], [ %6, %47 ], [ %6, %51 ]
%40 = call i32 (...) @getchar()
%41 = load i32, i32* @EOF
%42 = icmp eq i32 %40, %41
br i1 %42, label %57, label %26
43: ; preds = %26
%44 = getelementptr inbounds i8, i8* %28, i64 1
%45 = load i8, i8* %44
%46 = icmp eq i8 %45, 0
br i1 %46, label %47, label %38
47: ; preds = %43
%48 = call i32 @rewind(i32* %14)
%49 = call i64 @fread(i8* %4, i32 1, i32 512, i32* %14)
%50 = icmp eq i64 %49, 0
br i1 %50, label %38, label %51
51: ; preds = %47, %51
%52 = phi i64 [ %55, %51 ], [ %49, %47 ]
%53 = load i32, i32* @stdout
%54 = call i32 @fwrite(i8* %4, i32 1, i64 %52, i32 %53)
%55 = call i64 @fread(i8* %4, i32 1, i32 512, i32* %14)
%56 = icmp eq i64 %55, 0
br i1 %56, label %38, label %51
57: ; preds = %38, %16, %22
%58 = phi i32 [ 1, %22 ], [ 0, %16 ], [ 0, %38 ]
ret i32 %58
}