#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*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
scalar_t__ memmem(unsigned char *, int, unsigned char *, int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int findStrInFile(char *filename, int offset, unsigned char *str, int str_len) {
  int pos = 0, rc;
  FILE *fp;
  unsigned char mem[1024];
  if (str_len > 512)
    return -1;
  if (offset < 0)
    return -1;
  fp = fopen(filename, "rb");
  if (!fp)
    return -1;
  rewind(fp);
  fseek(fp, offset + pos, SEEK_SET);
  rc = fread(mem, 1, 1024, fp);
  while (rc) {
    unsigned char *mem_offset;
    mem_offset = (unsigned char *)memmem(mem, rc, str, str_len);
    if (mem_offset) {
      fclose(fp);
      return (mem_offset - mem) + pos + offset;
    }
    if (rc == 1024) {
      pos += 512;
    } else
      break;
    rewind(fp);
    fseek(fp, offset + pos, SEEK_SET);
    rc = fread(mem, 1, 1024, fp);
  }
  fclose(fp);
  return -1;
}


x86 assembly (clang, -O0):
<findStrInFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x200,-0x24(%rbp)
jle    1199 <findStrInFile+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <findStrInFile+0x192>
cmpl   $0x0,-0x14(%rbp)
jge    11af <findStrInFile+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <findStrInFile+0x192>
mov    -0x10(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11da <findStrInFile+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <findStrInFile+0x192>
mov    -0x38(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    -0x28(%rbp),%esi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
lea    -0x440(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     12e2 <findStrInFile+0x182>
lea    -0x440(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1060 <memmem@plt>
mov    %rax,-0x448(%rbp)
cmpq   $0x0,-0x448(%rbp)
je     127d <findStrInFile+0x11d>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x448(%rbp),%rax
lea    -0x440(%rbp),%rcx
sub    %rcx,%rax
movslq -0x28(%rbp),%rcx
add    %rcx,%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    12f2 <findStrInFile+0x192>
cmpl   $0x400,-0x2c(%rbp)
jne    129a <findStrInFile+0x13a>
mov    -0x28(%rbp),%eax
add    $0x200,%eax
mov    %eax,-0x28(%rbp)
jmp    129f <findStrInFile+0x13f>
jmp    12e2 <findStrInFile+0x182>
mov    -0x38(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    -0x28(%rbp),%esi
mov    0x2d27(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
lea    -0x440(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
jmp    1218 <findStrInFile+0xb8>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findStrInFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdx,0x8(%rsp)
mov    $0xffffffff,%ebx
test   %esi,%esi
js     128c <findStrInFile+0x12c>
mov    %ecx,%r15d
cmp    $0x200,%ecx
jg     128c <findStrInFile+0x12c>
mov    %esi,%r13d
lea    0xe64(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     128c <findStrInFile+0x12c>
mov    %rax,%r12
mov    %rax,%rdi
call   1050 <rewind@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %r13d,%esi
call   1070 <fseek@plt>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     126e <findStrInFile+0x10e>
mov    %eax,%r14d
lea    0x10(%rsp),%rbx
mov    %r13d,%eax
sub    %ebx,%eax
mov    %eax,0x4(%rsp)
add    $0x200,%r13d
mov    %rbx,%rbp
mov    %rbx,%rdi
mov    %r14d,%esi
mov    0x8(%rsp),%rdx
mov    %r15d,%ecx
call   1060 <memmem@plt>
test   %rax,%rax
jne    127d <findStrInFile+0x11d>
cmp    $0x400,%r14d
jne    126e <findStrInFile+0x10e>
mov    %rbp,%rbx
mov    %r12,%rdi
call   1050 <rewind@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %r13d,%esi
call   1070 <fseek@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
mov    %eax,%r14d
addl   $0x200,0x4(%rsp)
add    $0x200,%r13d
test   %eax,%eax
jne    1200 <findStrInFile+0xa0>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%ebx
jmp    128c <findStrInFile+0x12c>
mov    %rax,%rbx
mov    %r12,%rdi
call   1040 <fclose@plt>
add    0x4(%rsp),%ebx
mov    %ebx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_SET = global i32 0
define i32 @findStrInFile(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca [1024 x i8]
%14 = alloca i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 0, i32* %10
%15 = load i32, i32* %9
%16 = icmp sgt i32 %15, 512
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %89
18: ; preds = %4
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 -1, i32* %5
br label %89
22: ; preds = %18
%23 = load i8*, i8** %6
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %24, i32** %12
%25 = load i32*, i32** %12
%26 = icmp ne i32* %25, null
br i1 %26, label %28, label %27
27: ; preds = %22
store i32 -1, i32* %5
br label %89
28: ; preds = %22
%29 = load i32*, i32** %12
%30 = call i32 @rewind(i32* %29)
%31 = load i32*, i32** %12
%32 = load i32, i32* %7
%33 = load i32, i32* %10
%34 = add nsw i32 %32, %33
%35 = load i32, i32* @SEEK_SET
%36 = call i32 @fseek(i32* %31, i32 %34, i32 %35)
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%38 = load i32*, i32** %12
%39 = call i32 @fread(i8* %37, i32 1, i32 1024, i32* %38)
store i32 %39, i32* %11
br label %40
40: ; preds = %74, %28
%41 = load i32, i32* %11
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %86
43: ; preds = %40
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%45 = load i32, i32* %11
%46 = load i8*, i8** %8
%47 = load i32, i32* %9
%48 = call i64 @memmem(i8* %44, i32 %45, i8* %46, i32 %47)
%49 = inttoptr i64 %48 to i8*
store i8* %49, i8** %14
%50 = load i8*, i8** %14
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %67
52: ; preds = %43
%53 = load i32*, i32** %12
%54 = call i32 @fclose(i32* %53)
%55 = load i8*, i8** %14
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%57 = ptrtoint i8* %55 to i64
%58 = ptrtoint i8* %56 to i64
%59 = sub i64 %57, %58
%60 = load i32, i32* %10
%61 = sext i32 %60 to i64
%62 = add nsw i64 %59, %61
%63 = load i32, i32* %7
%64 = sext i32 %63 to i64
%65 = add nsw i64 %62, %64
%66 = trunc i64 %65 to i32
store i32 %66, i32* %5
br label %89
67: ; preds = %43
%68 = load i32, i32* %11
%69 = icmp eq i32 %68, 1024
br i1 %69, label %70, label %73
70: ; preds = %67
%71 = load i32, i32* %10
%72 = add nsw i32 %71, 512
store i32 %72, i32* %10
br label %74
73: ; preds = %67
br label %86
74: ; preds = %70
%75 = load i32*, i32** %12
%76 = call i32 @rewind(i32* %75)
%77 = load i32*, i32** %12
%78 = load i32, i32* %7
%79 = load i32, i32* %10
%80 = add nsw i32 %78, %79
%81 = load i32, i32* @SEEK_SET
%82 = call i32 @fseek(i32* %77, i32 %80, i32 %81)
%83 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%84 = load i32*, i32** %12
%85 = call i32 @fread(i8* %83, i32 1, i32 1024, i32* %84)
store i32 %85, i32* %11
br label %40
86: ; preds = %73, %40
%87 = load i32*, i32** %12
%88 = call i32 @fclose(i32* %87)
store i32 -1, i32* %5
br label %89
89: ; preds = %86, %52, %27, %21, %17
%90 = load i32, i32* %5
ret i32 %90
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_SET = global i32 0
define i32 @findStrInFile(i8* nocapture %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = icmp sgt i32 %3, 512
%8 = icmp slt i32 %1, 0
%9 = or i1 %8, %7
br i1 %9, label %43, label %10
10: ; preds = %4
%11 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %43, label %13
13: ; preds = %10
%14 = tail call i32 @rewind(i32* %11)
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @fseek(i32* %11, i32 %1, i32 %15)
%17 = call i32 @fread(i8* %6, i32 1, i32 1024, i32* %11)
%18 = icmp eq i32 %17, 0
br i1 %18, label %41, label %19
19: ; preds = %13, %33
%20 = phi i32 [ %34, %33 ], [ 0, %13 ]
%21 = phi i32 [ %39, %33 ], [ %17, %13 ]
%22 = call i64 @memmem(i8* %6, i32 %21, i8* %2, i32 %3)
%23 = icmp eq i64 %22, 0
br i1 %23, label %31, label %24
24: ; preds = %19
%25 = call i32 @fclose(i32* %11)
%26 = ptrtoint [1024 x i8]* %5 to i64
%27 = sub i64 %22, %26
%28 = trunc i64 %27 to i32
%29 = add i32 %20, %1
%30 = add i32 %29, %28
br label %43
31: ; preds = %19
%32 = icmp eq i32 %21, 1024
br i1 %32, label %33, label %41
33: ; preds = %31
%34 = add nuw nsw i32 %20, 512
%35 = call i32 @rewind(i32* %11)
%36 = add nsw i32 %34, %1
%37 = load i32, i32* @SEEK_SET
%38 = call i32 @fseek(i32* %11, i32 %36, i32 %37)
%39 = call i32 @fread(i8* %6, i32 1, i32 1024, i32* %11)
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %19
41: ; preds = %33, %31, %13
%42 = call i32 @fclose(i32* %11)
br label %43
43: ; preds = %24, %10, %4, %41
%44 = phi i32 [ -1, %41 ], [ -1, %4 ], [ -1, %10 ], [ %30, %24 ]
ret i32 %44
}