#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*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ *strstr(char *, char *);
int search(char *fname, char *str) {
  FILE *fp;
  int line_num = 1;
  int find_result = 0;
  char temp[512];
  if ((fp = fopen(fname, "r")) == NULL) {
    return (-1);
  }
  while (fgets(temp, 512, fp) != NULL) {
    if ((strstr(temp, str)) != NULL) {
      printf("A match found on line: %d\n", line_num);
      printf("\n%s\n", temp);
      return 1;
      find_result++;
    }
    line_num++;
  }
  if (find_result == 0) {
    printf("\nSorry, couldn't find a match.\n");
  }
  if (fp) {
    fclose(fp);
  }
  return (0);
}


x86 assembly (clang, -O0):
<search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x1,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe84(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    119b <search+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    124c <search+0xfc>
jmp    11a0 <search+0x50>
lea    -0x230(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x200,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1219 <search+0xc9>
lea    -0x230(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     120b <search+0xbb>
mov    -0x24(%rbp),%esi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x230(%rbp),%rsi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    124c <search+0xfc>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a0 <search+0x50>
cmpl   $0x0,-0x28(%rbp)
jne    1231 <search+0xe1>
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x20(%rbp)
je     1245 <search+0xf5>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%r14
lea    0xe89(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11f0 <search+0x90>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11d8 <search+0x78>
mov    $0x1,%ebp
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
jne    11f7 <search+0x97>
add    $0x1,%ebp
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    11b0 <search+0x50>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    121d <search+0xbd>
mov    $0xffffffff,%eax
jmp    121d <search+0xbd>
lea    0xe04(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe0f(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [27 x i8] c"A match found on line: %d\0A\00"
@.str.2 = [5 x i8] c"\0A%s\0A\00"
@.str.3 = [32 x i8] c"\0ASorry, couldn't find a match.\0A\00"
define i32 @search(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [512 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 1, i32* %7
store i32 0, i32* %8
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %45
14: ; preds = %2
br label %15
15: ; preds = %30, %14
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%17 = load i32*, i32** %6
%18 = call i32* @fgets(i8* %16, i32 512, i32* %17)
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %33
20: ; preds = %15
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%22 = load i8*, i8** %5
%23 = call i32* @strstr(i8* %21, i8* %22)
%24 = icmp ne i32* %23, null
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32, i32* %7
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %28)
store i32 1, i32* %3
br label %45
30: ; preds = %20
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %7
br label %15
33: ; preds = %15
%34 = load i32, i32* %8
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %33
%39 = load i32*, i32** %6
%40 = icmp ne i32* %39, null
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i32*, i32** %6
%43 = call i32 @fclose(i32* %42)
br label %44
44: ; preds = %41, %38
store i32 0, i32* %3
br label %45
45: ; preds = %44, %25, %13
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [27 x i8] c"A match found on line: %d\0A\00"
@.str.2 = [5 x i8] c"\0A%s\0A\00"
@str = [31 x i8] c"\0ASorry, couldn't find a match.\00"
define i32 @search(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %24, label %7
7: ; preds = %2
%8 = call i32* @fgets(i8* %4, i32 512, i32* %5)
%9 = icmp eq i32* %8, null
br i1 %9, label %21, label %10
10: ; preds = %7, %17
%11 = phi i32 [ %18, %17 ], [ 1, %7 ]
%12 = call i32* @strstr(i8* %4, i8* dereferenceable(1) %1)
%13 = icmp eq i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %10
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %24
17: ; preds = %10
%18 = add nuw nsw i32 %11, 1
%19 = call i32* @fgets(i8* %4, i32 512, i32* %5)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %10
21: ; preds = %17, %7
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%23 = call i32 @fclose(i32* %5)
br label %24
24: ; preds = %2, %21, %14
%25 = phi i32 [ 1, %14 ], [ 0, %21 ], [ -1, %2 ]
ret i32 %25
}