#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
scalar_t__ read(int, char *, int);
int strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int search_text_pattern(char *pattern, int all, const char *fname, int offset) {
  int fd;
  int page_count, patt_found;
  int patt_found_history = 0, len, i;
  char page[(4 * 1024) + (128)];
  int address = -1;
  if (fname == NULL)
    return -1;
  if (pattern == NULL)
    return -1;
  fd = open(fname, O_RDONLY);
  if (fd < 0)
    return -1;
  len = strlen(pattern);
  if (len >= (128))
    return -1;
  patt_found = 0;
  page_count = offset / (4 * 1024);
  i = offset % (4 * 1024);
  lseek(fd, page_count * (4 * 1024), SEEK_SET);
  printf(" Searching \"%s\" in %s...\n", pattern, fname);
  while (read(fd, page, (4 * 1024) + (128)) > 0) {
    for (; i < (4 * 1024); i++) {
      if (!strncmp((page + i), pattern, len)) {
        patt_found = 1;
        patt_found_history = 1;
      }
      if (patt_found == 1) {
        address = ((page_count * (4 * 1024)) + i);
        printf(" Found \'%s\' at offset: \"0x%X\"\n", pattern, address);
        if (!all) {
          goto exit_search;
        } else {
          patt_found = 0;
          i += len;
        }
      }
    }
    page_count++;
    i = 0;
    lseek(fd, page_count * (4 * 1024), SEEK_SET);
  }
  if (!patt_found_history)
    printf(" Could not find the pattern \'%s\'!\n", pattern);
exit_search:
  close(fd);
  return address;
}


x86 assembly (clang, -O0):
<search_text_pattern>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10d0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0xffffffff,-0x10c4(%rbp)
ff ff ff
cmpq   $0x0,-0x20(%rbp)
jne    11b1 <search_text_pattern+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    138f <search_text_pattern+0x21f>
cmpq   $0x0,-0x10(%rbp)
jne    11c8 <search_text_pattern+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    138f <search_text_pattern+0x21f>
mov    -0x20(%rbp),%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11f3 <search_text_pattern+0x83>
movl   $0xffffffff,-0x4(%rbp)
jmp    138f <search_text_pattern+0x21f>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x80,-0x38(%rbp)
jl     1218 <search_text_pattern+0xa8>
movl   $0xffffffff,-0x4(%rbp)
jmp    138f <search_text_pattern+0x21f>
movl   $0x0,-0x30(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x1000,%ecx
cltd
idiv   %ecx
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x1000,%ecx
cltd
idiv   %ecx
mov    %edx,-0x3c(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
shl    $0xc,%esi
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xd9f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%edi
lea    -0x10c0(%rbp),%rsi
mov    $0x1080,%edx
call   1080 <read@plt>
cmp    $0x0,%rax
jle    135d <search_text_pattern+0x1ed>
jmp    128b <search_text_pattern+0x11b>
cmpl   $0x1000,-0x3c(%rbp)
jge    1331 <search_text_pattern+0x1c1>
lea    -0x10c0(%rbp),%rdi
movslq -0x3c(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12c9 <search_text_pattern+0x159>
movl   $0x1,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
cmpl   $0x1,-0x30(%rbp)
jne    131e <search_text_pattern+0x1ae>
mov    -0x2c(%rbp),%eax
shl    $0xc,%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x10c4(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x10c4(%rbp),%edx
lea    0xd27(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1309 <search_text_pattern+0x199>
jmp    137e <search_text_pattern+0x20e>
movl   $0x0,-0x30(%rbp)
mov    -0x38(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    131e <search_text_pattern+0x1ae>
jmp    1323 <search_text_pattern+0x1b3>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    128b <search_text_pattern+0x11b>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
shl    $0xc,%esi
mov    0x2c87(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
jmp    1268 <search_text_pattern+0xf8>
cmpl   $0x0,-0x34(%rbp)
jne    1379 <search_text_pattern+0x209>
mov    -0x10(%rbp),%rsi
lea    0xcc7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    137e <search_text_pattern+0x20e>
mov    -0x28(%rbp),%edi
call   1070 <close@plt>
mov    -0x10c4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_text_pattern>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1098,%rsp
mov    %esi,0xc(%rsp)
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     1316 <search_text_pattern+0x1a6>
mov    %rdx,%r14
test   %rdx,%rdx
je     1316 <search_text_pattern+0x1a6>
mov    %ecx,%ebx
mov    %rdi,%r12
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1316 <search_text_pattern+0x1a6>
mov    %eax,%r15d
mov    %r12,%rdi
call   1040 <strlen@plt>
cmp    $0x7f,%eax
jg     1316 <search_text_pattern+0x1a6>
mov    %eax,%r13d
lea    0xfff(%rbx),%ebp
test   %ebx,%ebx
cmovns %ebx,%ebp
mov    %ebp,0x8(%rsp)
and    $0xfffff000,%ebp
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
mov    %ebp,%esi
call   1060 <lseek@plt>
lea    0xdfd(%rip),%rdi
mov    %r12,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x1080,%edx
call   1080 <read@plt>
test   %rax,%rax
jle    12f8 <search_text_pattern+0x188>
mov    %r15d,0x4(%rsp)
mov    0x8(%rsp),%r14d
sar    $0xc,%r14d
sub    %ebp,%ebx
mov    $0xffffffff,%ebp
xor    %r15d,%r15d
jmp    128f <search_text_pattern+0x11f>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x8(%rsp),%r14d
add    $0x1,%r14d
mov    %r14d,%esi
shl    $0xc,%esi
mov    0x2d72(%rip),%rax
mov    (%rax),%edx
mov    0x4(%rsp),%ebx
mov    %ebx,%edi
call   1060 <lseek@plt>
mov    %ebx,%edi
lea    0x10(%rsp),%rsi
mov    $0x1080,%edx
call   1080 <read@plt>
xor    %ebx,%ebx
test   %rax,%rax
jle    132a <search_text_pattern+0x1ba>
mov    %r14d,0x8(%rsp)
shl    $0xc,%r14d
jmp    12b0 <search_text_pattern+0x140>
nopw   0x0(%rax,%rax,1)
add    %r13d,%ebx
lea    0x1(%rbx),%eax
cmp    $0xfff,%ebx
mov    %eax,%ebx
jge    1250 <search_text_pattern+0xe0>
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
mov    $0x1,%eax
cmove  %eax,%r15d
jne    12a3 <search_text_pattern+0x133>
lea    (%rbx,%r14,1),%ebp
lea    0xd3c(%rip),%rdi
mov    %r12,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
cmpl   $0x0,0xc(%rsp)
jne    12a0 <search_text_pattern+0x130>
mov    0x4(%rsp),%r15d
jmp    130e <search_text_pattern+0x19e>
mov    $0xffffffff,%ebp
lea    0xd35(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15d,%edi
call   1070 <close@plt>
mov    %ebp,%eax
add    $0x1098,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
test   %r15d,%r15d
mov    0x4(%rsp),%r15d
jne    130e <search_text_pattern+0x19e>
jmp    12fd <search_text_pattern+0x18d>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@.str = [26 x i8] c" Searching \22%s\22 in %s...\0A\00"
@.str.1 = [31 x i8] c" Found '%s' at offset: \220x%X\22\0A\00"
@.str.2 = [35 x i8] c" Could not find the pattern '%s'!\0A\00"
define i32 @search_text_pattern(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 i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca [4224 x i8]
%17 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 0, i32* %13
store i32 -1, i32* %17
%18 = load i8*, i8** %8
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %4
store i32 -1, i32* %5
br label %112
21: ; preds = %4
%22 = load i8*, i8** %6
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 -1, i32* %5
br label %112
25: ; preds = %21
%26 = load i8*, i8** %8
%27 = load i32, i32* @O_RDONLY
%28 = call i32 @open(i8* %26, i32 %27)
store i32 %28, i32* %10
%29 = load i32, i32* %10
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25
store i32 -1, i32* %5
br label %112
32: ; preds = %25
%33 = load i8*, i8** %6
%34 = call i32 @strlen(i8* %33)
store i32 %34, i32* %14
%35 = load i32, i32* %14
%36 = icmp sge i32 %35, 128
br i1 %36, label %37, label %38
37: ; preds = %32
store i32 -1, i32* %5
br label %112
38: ; preds = %32
store i32 0, i32* %12
%39 = load i32, i32* %9
%40 = sdiv i32 %39, 4096
store i32 %40, i32* %11
%41 = load i32, i32* %9
%42 = srem i32 %41, 4096
store i32 %42, i32* %15
%43 = load i32, i32* %10
%44 = load i32, i32* %11
%45 = mul nsw i32 %44, 4096
%46 = load i32, i32* @SEEK_SET
%47 = call i32 @lseek(i32 %43, i32 %45, i32 %46)
%48 = load i8*, i8** %6
%49 = load i8*, i8** %8
%50 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %48, i8* %49)
br label %51
51: ; preds = %93, %38
%52 = load i32, i32* %10
%53 = getelementptr inbounds [4224 x i8], [4224 x i8]* %16, i64 0, i64 0
%54 = call i64 @read(i32 %52, i8* %53, i32 4224)
%55 = icmp sgt i64 %54, 0
br i1 %55, label %56, label %101
56: ; preds = %51
br label %57
57: ; preds = %90, %56
%58 = load i32, i32* %15
%59 = icmp slt i32 %58, 4096
br i1 %59, label %60, label %93
60: ; preds = %57
%61 = getelementptr inbounds [4224 x i8], [4224 x i8]* %16, i64 0, i64 0
%62 = load i32, i32* %15
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
%65 = load i8*, i8** %6
%66 = load i32, i32* %14
%67 = call i32 @strncmp(i8* %64, i8* %65, i32 %66)
%68 = icmp ne i32 %67, 0
br i1 %68, label %70, label %69
69: ; preds = %60
store i32 1, i32* %12
store i32 1, i32* %13
br label %70
70: ; preds = %69, %60
%71 = load i32, i32* %12
%72 = icmp eq i32 %71, 1
br i1 %72, label %73, label %89
73: ; preds = %70
%74 = load i32, i32* %11
%75 = mul nsw i32 %74, 4096
%76 = load i32, i32* %15
%77 = add nsw i32 %75, %76
store i32 %77, i32* %17
%78 = load i8*, i8** %6
%79 = load i32, i32* %17
%80 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %78, i32 %79)
%81 = load i32, i32* %7
%82 = icmp ne i32 %81, 0
br i1 %82, label %84, label %83
83: ; preds = %73
br label %108
84: ; preds = %73
store i32 0, i32* %12
%85 = load i32, i32* %14
%86 = load i32, i32* %15
%87 = add nsw i32 %86, %85
store i32 %87, i32* %15
br label %88
88: ; preds = %84
br label %89
89: ; preds = %88, %70
br label %90
90: ; preds = %89
%91 = load i32, i32* %15
%92 = add nsw i32 %91, 1
store i32 %92, i32* %15
br label %57
93: ; preds = %57
%94 = load i32, i32* %11
%95 = add nsw i32 %94, 1
store i32 %95, i32* %11
store i32 0, i32* %15
%96 = load i32, i32* %10
%97 = load i32, i32* %11
%98 = mul nsw i32 %97, 4096
%99 = load i32, i32* @SEEK_SET
%100 = call i32 @lseek(i32 %96, i32 %98, i32 %99)
br label %51
101: ; preds = %51
%102 = load i32, i32* %13
%103 = icmp ne i32 %102, 0
br i1 %103, label %107, label %104
104: ; preds = %101
%105 = load i8*, i8** %6
%106 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %105)
br label %107
107: ; preds = %104, %101
br label %108
108: ; preds = %107, %83
%109 = load i32, i32* %10
%110 = call i32 @close(i32 %109)
%111 = load i32, i32* %17
store i32 %111, i32* %5
br label %112
112: ; preds = %108, %37, %31, %24, %20
%113 = load i32, i32* %5
ret i32 %113
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@.str = [26 x i8] c" Searching \22%s\22 in %s...\0A\00"
@.str.1 = [31 x i8] c" Found '%s' at offset: \220x%X\22\0A\00"
@.str.2 = [35 x i8] c" Could not find the pattern '%s'!\0A\00"
define i32 @search_text_pattern(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca [4224 x i8]
%6 = getelementptr inbounds [4224 x i8], [4224 x i8]* %5, i64 0, i64 0
%7 = icmp eq i8* %2, null
%8 = icmp eq i8* %0, null
%9 = or i1 %8, %7
br i1 %9, label %68, label %10
10: ; preds = %4
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %2, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %68, label %14
14: ; preds = %10
%15 = tail call i32 @strlen(i8* %0)
%16 = icmp sgt i32 %15, 127
br i1 %16, label %68, label %17
17: ; preds = %14
%18 = sdiv i32 %3, 4096
%19 = srem i32 %3, 4096
%20 = shl nsw i32 %18, 12
%21 = load i32, i32* @SEEK_SET
%22 = tail call i32 @lseek(i32 %12, i32 %20, i32 %21)
%23 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %2)
%24 = call i64 @read(i32 %12, i8* %6, i32 4224)
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %62
26: ; preds = %17
%27 = icmp eq i32 %1, 0
br label %28
28: ; preds = %26, %53
%29 = phi i32 [ -1, %26 ], [ %50, %53 ]
%30 = phi i32 [ %19, %26 ], [ 0, %53 ]
%31 = phi i32 [ 0, %26 ], [ %42, %53 ]
%32 = phi i32 [ %18, %26 ], [ %54, %53 ]
%33 = shl nsw i32 %32, 12
br label %34
34: ; preds = %28, %48
%35 = phi i32 [ %29, %28 ], [ %50, %48 ]
%36 = phi i32 [ %30, %28 ], [ %51, %48 ]
%37 = phi i32 [ %31, %28 ], [ %42, %48 ]
%38 = sext i32 %36 to i64
%39 = getelementptr inbounds [4224 x i8], [4224 x i8]* %5, i64 0, i64 %38
%40 = call i32 @strncmp(i8* %39, i8* %0, i32 %15)
%41 = icmp eq i32 %40, 0
%42 = select i1 %41, i32 1, i32 %37
br i1 %41, label %43, label %48
43: ; preds = %34
%44 = add nsw i32 %36, %33
%45 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %44)
br i1 %27, label %65, label %46
46: ; preds = %43
%47 = add nsw i32 %36, %15
br label %48
48: ; preds = %34, %46
%49 = phi i32 [ %47, %46 ], [ %36, %34 ]
%50 = phi i32 [ %44, %46 ], [ %35, %34 ]
%51 = add nsw i32 %49, 1
%52 = icmp slt i32 %49, 4095
br i1 %52, label %34, label %53
53: ; preds = %48
%54 = add nsw i32 %32, 1
%55 = shl nsw i32 %54, 12
%56 = load i32, i32* @SEEK_SET
%57 = call i32 @lseek(i32 %12, i32 %55, i32 %56)
%58 = call i64 @read(i32 %12, i8* %6, i32 4224)
%59 = icmp sgt i64 %58, 0
br i1 %59, label %28, label %60
60: ; preds = %53
%61 = icmp eq i32 %42, 0
br i1 %61, label %62, label %65
62: ; preds = %17, %60
%63 = phi i32 [ %50, %60 ], [ -1, %17 ]
%64 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %65
65: ; preds = %43, %60, %62
%66 = phi i32 [ %50, %60 ], [ %63, %62 ], [ %44, %43 ]
%67 = call i32 @close(i32 %12)
br label %68
68: ; preds = %14, %10, %4, %65
%69 = phi i32 [ %66, %65 ], [ -1, %4 ], [ -1, %10 ], [ -1, %14 ]
ret i32 %69
}