#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*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
char *strstr(char *, char *);
int proc_num_of_occ(char *file, char *keyword) {
  FILE *fp;
  char buffer[1024];
  char *buffptr = (char *)buffer;
  size_t blocks_read;
  unsigned int occ = 0;
  if ((fp = fopen(file, "r")) == NULL) {
    perror("fopen()");
    exit(EXIT_FAILURE);
  }
  blocks_read = fread(buffer, sizeof(buffer), 1, fp);
  do {
    buffptr = (char *)buffer;
    buffptr = strstr(buffptr, keyword);
    while (buffptr != NULL) {
      buffptr++;
      occ++;
      buffptr = strstr(buffptr, keyword);
    }
    blocks_read = fread(buffer, sizeof(buffer), 1, fp);
  } while (blocks_read > 0);
  fclose(fp);
  return occ;
}


x86 assembly (clang, -O0):
<proc_num_of_occ>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
movl   $0x0,-0x434(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11c3 <proc_num_of_occ+0x63>
lea    0xe52(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x400,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %rax,-0x430(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
je     1254 <proc_num_of_occ+0xf4>
mov    -0x428(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x428(%rbp)
mov    -0x434(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x434(%rbp)
mov    -0x428(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x428(%rbp)
jmp    1209 <proc_num_of_occ+0xa9>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x400,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
ja     11e4 <proc_num_of_occ+0x84>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x434(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_num_of_occ>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%rbx
lea    0xe89(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11f1 <proc_num_of_occ+0x91>
mov    %rax,%r14
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x400,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
xor    %ebp,%ebp
jmp    11ba <proc_num_of_occ+0x5a>
mov    %r15,%rdi
mov    $0x400,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11d9 <proc_num_of_occ+0x79>
mov    %r15,%rdi
nopl   (%rax)
mov    %rbx,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     11a0 <proc_num_of_occ+0x40>
add    $0x1,%rax
add    $0x1,%ebp
mov    %rax,%rdi
jmp    11c0 <proc_num_of_occ+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0a(%rip),%rdi
call   1060 <perror@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [8 x i8] c"fopen()\00"
@EXIT_FAILURE = global i32 0
define i32 @proc_num_of_occ(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [1024 x i8]
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
store i8* %10, i8** %7
store i32 0, i32* %9
%11 = load i8*, i8** %3
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %5
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%20 = load i32*, i32** %5
%21 = call i64 @fread(i8* %19, i32 1024, i32 1, i32* %20)
store i64 %21, i64* %8
br label %22
22: ; preds = %42, %18
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = load i8*, i8** %4
%26 = call i8* @strstr(i8* %24, i8* %25)
store i8* %26, i8** %7
br label %27
27: ; preds = %30, %22
%28 = load i8*, i8** %7
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %38
30: ; preds = %27
%31 = load i8*, i8** %7
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %7
%33 = load i32, i32* %9
%34 = add i32 %33, 1
store i32 %34, i32* %9
%35 = load i8*, i8** %7
%36 = load i8*, i8** %4
%37 = call i8* @strstr(i8* %35, i8* %36)
store i8* %37, i8** %7
br label %27
38: ; preds = %27
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%40 = load i32*, i32** %5
%41 = call i64 @fread(i8* %39, i32 1024, i32 1, i32* %40)
store i64 %41, i64* %8
br label %42
42: ; preds = %38
%43 = load i64, i64* %8
%44 = icmp ugt i64 %43, 0
br i1 %44, label %22, label %45
45: ; preds = %42
%46 = load i32*, i32** %5
%47 = call i32 @fclose(i32* %46)
%48 = load i32, i32* %9
ret i32 %48
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [8 x i8] c"fopen()\00"
@EXIT_FAILURE = global i32 0
define i32 @proc_num_of_occ(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 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 %7, label %11
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = call i64 @fread(i8* %4, i32 1024, i32 1, i32* %5)
br label %13
13: ; preds = %24, %11
%14 = phi i32 [ 0, %11 ], [ %25, %24 ]
%15 = call i8* @strstr(i8* %4, i8* dereferenceable(1) %1)
%16 = icmp eq i8* %15, null
br i1 %16, label %24, label %17
17: ; preds = %13, %17
%18 = phi i32 [ %21, %17 ], [ %14, %13 ]
%19 = phi i8* [ %22, %17 ], [ %15, %13 ]
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = add i32 %18, 1
%22 = call i8* @strstr(i8* %20, i8* dereferenceable(1) %1)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %17
24: ; preds = %17, %13
%25 = phi i32 [ %14, %13 ], [ %21, %17 ]
%26 = call i64 @fread(i8* %4, i32 1024, i32 1, i32* %5)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %13
28: ; preds = %24
%29 = call i32 @fclose(i32* %5)
ret i32 %25
}