#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 */
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *, int, char *);
scalar_t__ strstr(char *, char *);
void search_file(char *pattern, char *filename, FILE *fp) {
  char line[10000];
  int line_number = 0;
  while (fgets(line, 10000, fp)) {
    line_number++;
    if (strstr(line, pattern)) {
      printf("[%s] line %d: %s\n", filename, line_number, line);
    }
  }
}


x86 assembly (clang, -O0):
<search_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2740,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x2734(%rbp)
lea    -0x2730(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x2710,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11bb <search_file+0x8b>
mov    -0x2734(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2734(%rbp)
lea    -0x2730(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <strstr@plt>
cmp    $0x0,%rax
je     11b6 <search_file+0x86>
mov    -0x10(%rbp),%rsi
mov    -0x2734(%rbp),%edx
lea    -0x2730(%rbp),%rcx
lea    0xe4f(%rip),%rdi
call   1030 <printf@plt>
jmp    1151 <search_file+0x21>
add    $0x2740,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2718,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%rdi
mov    $0x2710,%esi
call   1040 <fgets@plt>
test   %rax,%rax
je     11aa <search_file+0x7a>
mov    $0x1,%ebp
mov    %rsp,%rbx
lea    0xe95(%rip),%r13
jmp    1188 <search_file+0x58>
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x2710,%esi
mov    %r12,%rdx
call   1040 <fgets@plt>
add    $0x1,%ebp
test   %rax,%rax
je     11aa <search_file+0x7a>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     1170 <search_file+0x40>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1030 <printf@plt>
jmp    1170 <search_file+0x40>
add    $0x2718,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"[%s] line %d: %s\0A\00"
define void @search_file(i8* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [10000 x i8]
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* %2, i32** %6
store i32 0, i32* %8
br label %9
9: ; preds = %26, %3
%10 = getelementptr inbounds [10000 x i8], [10000 x i8]* %7, i64 0, i64 0
%11 = load i32*, i32** %6
%12 = call i64 @fgets(i8* %10, i32 10000, i32* %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %9
%15 = load i32, i32* %8
%16 = add nsw i32 %15, 1
store i32 %16, i32* %8
%17 = getelementptr inbounds [10000 x i8], [10000 x i8]* %7, i64 0, i64 0
%18 = load i8*, i8** %4
%19 = call i64 @strstr(i8* %17, i8* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %14
%22 = load i8*, i8** %5
%23 = load i32, i32* %8
%24 = getelementptr inbounds [10000 x i8], [10000 x i8]* %7, i64 0, i64 0
%25 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %22, i32 %23, i8* %24)
br label %26
26: ; preds = %21, %14
br label %9
27: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [18 x i8] c"[%s] line %d: %s\0A\00"
define void @search_file(i8* %0, i8* %1, i32* nocapture %2) {
%4 = alloca [10000 x i8]
%5 = getelementptr inbounds [10000 x i8], [10000 x i8]* %4, i64 0, i64 0
%6 = call i64 @fgets(i8* %5, i32 10000, i32* %2)
%7 = icmp eq i64 %6, 0
br i1 %7, label %18, label %8
8: ; preds = %3, %15
%9 = phi i32 [ %10, %15 ], [ 0, %3 ]
%10 = add nuw nsw i32 %9, 1
%11 = call i64 @strstr(i8* %5, i8* %0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %8
%14 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %10, i8* %5)
br label %15
15: ; preds = %13, %8
%16 = call i64 @fgets(i8* %5, i32 10000, i32* %2)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %8
18: ; preds = %15, %3
ret void
}