#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ *strstr(char *, char *);
int find_pattern(FILE *fp, char pat[], int except, int number, char *name) {
  char line[1000];
  long int linenum = 0;
  int found = 0;
  int first = 0;
  static int first_file = 0;
  while ((fgets(line, 1000, fp) != NULL)) {
    linenum++;
    if (first == 0 && fp != stdin) {
      if (first_file == 0) {
        first_file = 1;
      } else {
        printf("====================================\n\n");
      }
      printf("In File: %s\n", name);
      first = 1;
    }
    if ((strstr(line, pat) != NULL) != except) {
      if (number) {
        printf("%ld ", linenum);
      }
    }
    printf("%s\n", line);
    found++;
  }
  fclose(fp);
  return found;
}


x86 assembly (clang, -O0):
<find_pattern>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
movq   $0x0,-0x418(%rbp)
movl   $0x0,-0x41c(%rbp)
movl   $0x0,-0x420(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x3e8,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1286 <find_pattern+0x146>
mov    -0x418(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x418(%rbp)
cmpl   $0x0,-0x420(%rbp)
jne    1214 <find_pattern+0xd4>
mov    -0x8(%rbp),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
je     1214 <find_pattern+0xd4>
cmpl   $0x0,0x2e73(%rip)
jne    11ea <find_pattern+0xaa>
movl   $0x1,0x2e63(%rip)
jmp    11f8 <find_pattern+0xb8>
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x420(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strstr@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
cmp    -0x14(%rbp),%eax
je     125d <find_pattern+0x11d>
cmpl   $0x0,-0x18(%rbp)
je     1258 <find_pattern+0x118>
mov    -0x418(%rbp),%rsi
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125d <find_pattern+0x11d>
lea    -0x410(%rbp),%rsi
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x41c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x41c(%rbp)
jmp    117c <find_pattern+0x3c>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x41c(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_pattern>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3f8,%rsp
mov    %r8,0x8(%rsp)
mov    %ecx,%r12d
mov    %edx,0x4(%rsp)
mov    %rsi,%r13
mov    %rdi,%rbx
lea    0x10(%rsp),%rdi
mov    $0x3e8,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1252 <find_pattern+0x102>
lea    0x10(%rsp),%r14
xor    %r15d,%r15d
xor    %ebp,%ebp
jmp    11c1 <find_pattern+0x71>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
mov    $0x3e8,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1254 <find_pattern+0x104>
mov    %r15d,%eax
mov    $0x1,%r15d
test   %eax,%eax
jne    1210 <find_pattern+0xc0>
mov    0x2e13(%rip),%rax
cmp    %rbx,(%rax)
je     11f1 <find_pattern+0xa1>
cmpb   $0x0,0x2e6f(%rip)
je     11f6 <find_pattern+0xa6>
lea    0xe28(%rip),%rdi
call   1030 <puts@plt>
jmp    11fd <find_pattern+0xad>
xor    %r15d,%r15d
jmp    1210 <find_pattern+0xc0>
movb   $0x1,0x2e53(%rip)
lea    0xdfc(%rip),%rdi
mov    0x8(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbp
mov    %r14,%rdi
mov    %r13,%rsi
call   1070 <strstr@plt>
xor    %ecx,%ecx
test   %rax,%rax
setne  %al
test   %r12d,%r12d
je     11a0 <find_pattern+0x50>
mov    %al,%cl
cmp    0x4(%rsp),%ecx
je     11a0 <find_pattern+0x50>
lea    0xdca(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11a0 <find_pattern+0x50>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x3f8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@find_pattern.first_file = internal global i32 0
@stdin = global i32* null
@.str = [39 x i8] c"====================================\0A\0A\00"
@.str.1 = [13 x i8] c"In File: %s\0A\00"
@.str.2 = [5 x i8] c"%ld \00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @find_pattern(i32* %0, i8* %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca [1000 x i8]
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
store i32* %0, i32** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i8* %4, i8** %10
store i64 0, i64* %12
store i32 0, i32* %13
store i32 0, i32* %14
br label %15
15: ; preds = %53, %5
%16 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%17 = load i32*, i32** %6
%18 = call i32* @fgets(i8* %16, i32 1000, i32* %17)
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %58
20: ; preds = %15
%21 = load i64, i64* %12
%22 = add nsw i64 %21, 1
store i64 %22, i64* %12
%23 = load i32, i32* %14
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %38
25: ; preds = %20
%26 = load i32*, i32** %6
%27 = load i32*, i32** @stdin
%28 = icmp ne i32* %26, %27
br i1 %28, label %29, label %38
29: ; preds = %25
%30 = load i32, i32* @find_pattern.first_file
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 1, i32* @find_pattern.first_file
br label %35
33: ; preds = %29
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %35
35: ; preds = %33, %32
%36 = load i8*, i8** %10
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %36)
store i32 1, i32* %14
br label %38
38: ; preds = %35, %25, %20
%39 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%40 = load i8*, i8** %7
%41 = call i32* @strstr(i8* %39, i8* %40)
%42 = icmp ne i32* %41, null
%43 = zext i1 %42 to i32
%44 = load i32, i32* %8
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %53
46: ; preds = %38
%47 = load i32, i32* %9
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i64, i64* %12
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %50)
br label %52
52: ; preds = %49, %46
br label %53
53: ; preds = %52, %38
%54 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %54)
%56 = load i32, i32* %13
%57 = add nsw i32 %56, 1
store i32 %57, i32* %13
br label %15
58: ; preds = %15
%59 = load i32*, i32** %6
%60 = call i32 @fclose(i32* %59)
%61 = load i32, i32* %13
ret i32 %61
}

LLVM IR (-O2):
@find_pattern.first_file = internal unnamed_addr global i1 false
@stdin = global i32* null
@.str.1 = [13 x i8] c"In File: %s\0A\00"
@.str.2 = [5 x i8] c"%ld \00"
@str = [38 x i8] c"====================================\0A\00"
define i32 @find_pattern(i32* nocapture %0, i8* nocapture %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca [1000 x i8]
%7 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%8 = call i32* @fgets(i8* %7, i32 1000, i32* %0)
%9 = icmp eq i32* %8, null
br i1 %9, label %42, label %10
10: ; preds = %5
%11 = icmp ne i32 %3, 0
br label %12
12: ; preds = %10, %36
%13 = phi i32 [ 0, %10 ], [ %28, %36 ]
%14 = phi i64 [ 0, %10 ], [ %15, %36 ]
%15 = add nuw nsw i64 %14, 1
%16 = icmp eq i32 %13, 0
br i1 %16, label %17, label %27
17: ; preds = %12
%18 = load i32*, i32** @stdin
%19 = icmp eq i32* %18, %0
br i1 %19, label %27, label %20
20: ; preds = %17
%21 = load i1, i1* @find_pattern.first_file
br i1 %21, label %23, label %22
22: ; preds = %20
store i1 true, i1* @find_pattern.first_file
br label %25
23: ; preds = %20
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %23, %22
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %27
27: ; preds = %25, %17, %12
%28 = phi i32 [ 1, %25 ], [ 0, %17 ], [ 1, %12 ]
%29 = call i32* @strstr(i8* %7, i8* dereferenceable(1) %1)
%30 = icmp ne i32* %29, null
%31 = zext i1 %30 to i32
%32 = icmp ne i32 %31, %2
%33 = and i1 %11, %32
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %15)
br label %36
36: ; preds = %34, %27
%37 = call i32 @puts(i8* %7)
%38 = call i32* @fgets(i8* %7, i32 1000, i32* %0)
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %12
40: ; preds = %36
%41 = trunc i64 %15 to i32
br label %42
42: ; preds = %40, %5
%43 = phi i32 [ 0, %5 ], [ %41, %40 ]
%44 = call i32 @fclose(i32* %0)
ret i32 %43
}