#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*/ *strstr(char *, char *);
int grep(char *s, char *filename) {
  FILE *f;
  char line[4096];
  int present;
  present = 0;
  f = fopen(filename, "r");
  while (fgets(line, 4096, f) != NULL)
    if (strstr(line, s) != NULL) {
      fclose(f);
      return (1);
    }
  fclose(f);
  return (0);
}


x86 assembly (clang, -O0):
<grep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1024(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe98(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11c4 <grep+0x84>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strstr@plt>
cmp    $0x0,%rax
je     11bf <grep+0x7f>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    11d4 <grep+0x94>
jmp    1171 <grep+0x31>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grep>:
push   %r15
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %rsi,%rax
mov    %rdi,%r14
lea    0xea7(%rip),%rsi
mov    %rax,%rdi
call   1050 <fopen@plt>
mov    %rax,%r15
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     119c <grep+0x5c>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     1170 <grep+0x30>
mov    $0x1,%ebx
jmp    119e <grep+0x5e>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @grep(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [4096 x i8]
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %8
%9 = load i8*, i8** %5
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
br label %11
11: ; preds = %24, %2
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%13 = load i32*, i32** %6
%14 = call i32* @fgets(i8* %12, i32 4096, i32* %13)
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %25
16: ; preds = %11
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%18 = load i8*, i8** %4
%19 = call i32* @strstr(i8* %17, i8* %18)
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32*, i32** %6
%23 = call i32 @fclose(i32* %22)
store i32 1, i32* %3
br label %28
24: ; preds = %16
br label %11
25: ; preds = %11
%26 = load i32*, i32** %6
%27 = call i32 @fclose(i32* %26)
store i32 0, i32* %3
br label %28
28: ; preds = %25, %21
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @grep(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %9, %2
%7 = call i32* @fgets(i8* %4, i32 4096, i32* %5)
%8 = icmp eq i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = call i32* @strstr(i8* %4, i8* dereferenceable(1) %0)
%11 = icmp eq i32* %10, null
br i1 %11, label %6, label %12
12: ; preds = %6, %9
%13 = phi i32 [ 1, %9 ], [ 0, %6 ]
%14 = call i32 @fclose(i32* %5)
ret i32 %13
}