#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
char *strstr(char *, char *);
int main(int argc, char *argv[]) {
  if (argc < 4) {
    return EXIT_FAILURE;
  }
  FILE *infptr;
  infptr = fopen(argv[1], "r");
  if (infptr == NULL) {
    return EXIT_FAILURE;
  }
  FILE *outfptr;
  outfptr = fopen(argv[2], "w");
  if (outfptr == NULL) {
    fclose(infptr);
    return EXIT_FAILURE;
  }
  int count = 0;
  char oneline[81];
  while (fgets(oneline, 81, infptr) != NULL) {
    if (strstr(oneline, argv[3]) != NULL) {
      fprintf(outfptr, "%s", oneline);
    }
    char *chptr = oneline;
    while (chptr != NULL) {
      chptr = strstr(chptr, argv[3]);
      if (chptr != NULL) {
        count++;
        chptr++;
      }
    }
  }
  fprintf(outfptr, "%d\n", count);
  fclose(infptr);
  fclose(outfptr);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    1184 <main+0x34>
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12e3 <main+0x193>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe6d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b8 <main+0x68>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12e3 <main+0x193>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe3b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f5 <main+0xa5>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12e3 <main+0x193>
movl   $0x0,-0x24(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x51,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     12b0 <main+0x160>
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     1249 <main+0xf9>
mov    -0x20(%rbp),%rdi
lea    -0x80(%rbp),%rdx
lea    0xdc2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x80(%rbp),%rax
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x88(%rbp)
je     12ab <main+0x15b>
mov    -0x88(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x88(%rbp)
je     12a6 <main+0x156>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x88(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x88(%rbp)
jmp    1254 <main+0x104>
jmp    11fc <main+0xac>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xd49(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
cmp    $0x3,%edi
jle    1279 <main+0x119>
mov    %rsi,%r12
mov    0x8(%rsi),%rdi
lea    0xe7b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     1279 <main+0x119>
mov    0x10(%r12),%rdi
lea    0xe5f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     1282 <main+0x122>
lea    0x10(%rsp),%r13
mov    %r13,%rdi
mov    $0x51,%esi
mov    (%rsp),%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1294 <main+0x134>
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
mov    0x18(%r12),%rbx
mov    %r13,%rdi
mov    %rbx,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     1207 <main+0xa7>
mov    %r13,%rdi
mov    0x8(%rsp),%rsi
call   1040 <fputs@plt>
mov    0x18(%r12),%rbx
add    $0xffffffff,%r15d
mov    %r15d,%r14d
mov    %r13,%rdi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rsi
call   1080 <strstr@plt>
lea    0x1(%rax),%rdi
add    $0x1,%r14d
test   %rax,%rax
jne    1220 <main+0xc0>
mov    %rax,%rbp
mov    %r13,%rdi
mov    $0x51,%esi
mov    (%rsp),%rdx
call   1050 <fgets@plt>
cmp    $0x1,%rbp
mov    $0x1,%cl
adc    $0x0,%cl
movzbl %cl,%r15d
and    $0x1,%r15d
add    %r14d,%r15d
test   %rax,%rax
jne    11e0 <main+0x80>
cmp    $0x1,%rbp
mov    $0x1,%al
adc    $0x0,%al
movzbl %al,%edx
and    $0x1,%edx
add    %r14d,%edx
jmp    1296 <main+0x136>
mov    0x2d60(%rip),%rax
jmp    12c4 <main+0x164>
mov    (%rsp),%rdi
call   1030 <fclose@plt>
mov    0x2d4e(%rip),%rax
jmp    12c4 <main+0x164>
xor    %edx,%edx
lea    0xd67(%rip),%rsi
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rsp),%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [81 x i8]
%4 = icmp slt i32 %0, 4
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = load i32, i32* @EXIT_FAILURE
br label %56
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = load i32, i32* @EXIT_FAILURE
br label %56
14: ; preds = %7
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = tail call i32 @fclose(i32* %10)
%21 = load i32, i32* @EXIT_FAILURE
br label %56
22: ; preds = %14
%23 = getelementptr inbounds [81 x i8], [81 x i8]* %3, i64 0, i64 0
%24 = call i32* @fgets(i8* %23, i32 81, i32* %10)
%25 = icmp eq i32* %24, null
br i1 %25, label %50, label %26
26: ; preds = %22
%27 = getelementptr inbounds i8*, i8** %1, i64 3
br label %31
28: ; preds = %41
%29 = call i32* @fgets(i8* %23, i32 81, i32* %10)
%30 = icmp eq i32* %29, null
br i1 %30, label %50, label %31
31: ; preds = %26, %28
%32 = phi i32 [ 0, %26 ], [ %49, %28 ]
%33 = load i8*, i8** %27
%34 = call i8* @strstr(i8* %23, i8* dereferenceable(1) %33)
%35 = icmp eq i8* %34, null
br i1 %35, label %39, label %36
36: ; preds = %31
%37 = call i32 @fputs(i8* %23, i32* %17)
%38 = load i8*, i8** %27
br label %39
39: ; preds = %36, %31
%40 = phi i8* [ %38, %36 ], [ %33, %31 ]
br label %41
41: ; preds = %39, %41
%42 = phi i8* [ %23, %39 ], [ %46, %41 ]
%43 = phi i32 [ %32, %39 ], [ %49, %41 ]
%44 = call i8* @strstr(i8* %42, i8* dereferenceable(1) %40)
%45 = icmp eq i8* %44, null
%46 = getelementptr inbounds i8, i8* %44, i64 1
%47 = xor i1 %45, true
%48 = zext i1 %47 to i32
%49 = add nsw i32 %43, %48
br i1 %45, label %28, label %41
50: ; preds = %28, %22
%51 = phi i32 [ 0, %22 ], [ %49, %28 ]
%52 = call i32 (i32*, i8*, ...) @fprintf(i32* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %51)
%53 = call i32 @fclose(i32* %10)
%54 = call i32 @fclose(i32* %17)
%55 = load i32, i32* @EXIT_SUCCESS
br label %56
56: ; preds = %12, %50, %19, %5
%57 = phi i32 [ %6, %5 ], [ %13, %12 ], [ %21, %19 ], [ %55, %50 ]
ret i32 %57
}