#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 scalar_t__ uint8_t;
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
ssize_t find_frag(uint8_t *f, size_t l, size_t foff, uint8_t *frag,
                  size_t fraglen) {
  size_t i = 0;
  size_t off = 0;
  size_t start = 0;
  uint8_t found = 0;
  while (!found && (i < l)) {
    if (frag[off] != f[i]) {
      off = 0;
    } else {
      if (off == (fraglen - 1)) {
        found = 1;
        break;
      } else if (off == 0) {
        start = i;
      }
      off++;
    }
    i++;
  }
  if (found) {
    printf("FOUND STARTING AT +%lx (%lx)\n", (long unsigned)start,
           (long unsigned)start + foff);
    return start + foff;
  } else {
    printf("NOT FOUND\n");
    return -1;
  }
}


x86 assembly (clang, -O0):
<find_frag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x50(%rbp)
mov    %al,-0x51(%rbp)
jne    116a <find_frag+0x5a>
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
setb   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    117a <find_frag+0x6a>
jmp    11fc <find_frag+0xec>
mov    -0x28(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x10(%rbp),%rcx
mov    -0x38(%rbp),%rdx
cmp    (%rcx,%rdx,8),%rax
je     11a5 <find_frag+0x95>
movq   $0x0,-0x40(%rbp)
jmp    11eb <find_frag+0xdb>
mov    -0x40(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jne    11c7 <find_frag+0xb7>
movq   $0x1,-0x50(%rbp)
jmp    11fc <find_frag+0xec>
cmpq   $0x0,-0x40(%rbp)
jne    11da <find_frag+0xca>
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    11df <find_frag+0xcf>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    114c <find_frag+0x3c>
cmpq   $0x0,-0x50(%rbp)
je     1232 <find_frag+0x122>
mov    -0x48(%rbp),%rsi
mov    -0x48(%rbp),%rdx
add    -0x20(%rbp),%rdx
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x48(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1248 <find_frag+0x138>
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_frag>:
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     1167 <find_frag+0x47>
mov    %rdx,%r14
mov    %rsi,%r9
add    $0xffffffffffffffff,%r8
xor    %esi,%esi
xor    %ebx,%ebx
xor    %edx,%edx
jmp    114b <find_frag+0x2b>
nopl   0x0(%rax,%rax,1)
xor    %ebx,%ebx
add    $0x1,%rdx
cmp    %rdx,%r9
je     1167 <find_frag+0x47>
mov    (%rcx,%rbx,8),%rax
cmp    (%rdi,%rdx,8),%rax
jne    1140 <find_frag+0x20>
cmp    %r8,%rbx
je     117c <find_frag+0x5c>
test   %rbx,%rbx
cmove  %rdx,%rsi
add    $0x1,%rbx
jmp    1142 <find_frag+0x22>
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffffffffffff,%r14
jmp    1190 <find_frag+0x70>
add    %rsi,%r14
lea    0xe7a(%rip),%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [30 x i8] c"FOUND STARTING AT +%lx (%lx)\0A\00"
@.str.1 = [11 x i8] c"NOT FOUND\0A\00"
define i64 @find_frag(i64* %0, i64 %1, i64 %2, i64* %3, i64 %4) {
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64*
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
store i64* %0, i64** %7
store i64 %1, i64* %8
store i64 %2, i64* %9
store i64* %3, i64** %10
store i64 %4, i64* %11
store i64 0, i64* %12
store i64 0, i64* %13
store i64 0, i64* %14
store i64 0, i64* %15
br label %16
16: ; preds = %51, %5
%17 = load i64, i64* %15
%18 = icmp ne i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = load i64, i64* %12
%21 = load i64, i64* %8
%22 = icmp ult i64 %20, %21
br label %23
23: ; preds = %19, %16
%24 = phi i1 [ false, %16 ], [ %22, %19 ]
br i1 %24, label %25, label %54
25: ; preds = %23
%26 = load i64*, i64** %10
%27 = load i64, i64* %13
%28 = getelementptr inbounds i64, i64* %26, i64 %27
%29 = load i64, i64* %28
%30 = load i64*, i64** %7
%31 = load i64, i64* %12
%32 = getelementptr inbounds i64, i64* %30, i64 %31
%33 = load i64, i64* %32
%34 = icmp ne i64 %29, %33
br i1 %34, label %35, label %36
35: ; preds = %25
store i64 0, i64* %13
br label %51
36: ; preds = %25
%37 = load i64, i64* %13
%38 = load i64, i64* %11
%39 = sub i64 %38, 1
%40 = icmp eq i64 %37, %39
br i1 %40, label %41, label %42
41: ; preds = %36
store i64 1, i64* %15
br label %54
42: ; preds = %36
%43 = load i64, i64* %13
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = load i64, i64* %12
store i64 %46, i64* %14
br label %47
47: ; preds = %45, %42
br label %48
48: ; preds = %47
%49 = load i64, i64* %13
%50 = add i64 %49, 1
store i64 %50, i64* %13
br label %51
51: ; preds = %48, %35
%52 = load i64, i64* %12
%53 = add i64 %52, 1
store i64 %53, i64* %12
br label %16
54: ; preds = %41, %23
%55 = load i64, i64* %15
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %66
57: ; preds = %54
%58 = load i64, i64* %14
%59 = load i64, i64* %14
%60 = load i64, i64* %9
%61 = add i64 %59, %60
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i64 %58, i64 %61)
%63 = load i64, i64* %14
%64 = load i64, i64* %9
%65 = add i64 %63, %64
store i64 %65, i64* %6
br label %68
66: ; preds = %54
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i64 -1, i64* %6
br label %68
68: ; preds = %66, %57
%69 = load i64, i64* %6
ret i64 %69
}

LLVM IR (-O2):
@.str = [30 x i8] c"FOUND STARTING AT +%lx (%lx)\0A\00"
@str = [10 x i8] c"NOT FOUND\00"
define i64 @find_frag(i64* nocapture %0, i64 %1, i64 %2, i64* nocapture %3, i64 %4) {
%6 = icmp eq i64 %1, 0
br i1 %6, label %32, label %7
7: ; preds = %5
%8 = add i64 %4, -1
br label %9
9: ; preds = %7, %24
%10 = phi i64 [ 0, %7 ], [ %26, %24 ]
%11 = phi i64 [ 0, %7 ], [ %25, %24 ]
%12 = phi i64 [ 0, %7 ], [ %27, %24 ]
%13 = getelementptr inbounds i64, i64* %3, i64 %11
%14 = load i64, i64* %13
%15 = getelementptr inbounds i64, i64* %0, i64 %12
%16 = load i64, i64* %15
%17 = icmp eq i64 %14, %16
br i1 %17, label %18, label %24
18: ; preds = %9
%19 = icmp eq i64 %11, %8
br i1 %19, label %29, label %20
20: ; preds = %18
%21 = icmp eq i64 %11, 0
%22 = select i1 %21, i64 %12, i64 %10
%23 = add i64 %11, 1
br label %24
24: ; preds = %9, %20
%25 = phi i64 [ %23, %20 ], [ 0, %9 ]
%26 = phi i64 [ %22, %20 ], [ %10, %9 ]
%27 = add nuw i64 %12, 1
%28 = icmp eq i64 %27, %1
br i1 %28, label %32, label %9
29: ; preds = %18
%30 = add i64 %10, %2
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i64 %10, i64 %30)
br label %34
32: ; preds = %24, %5
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = phi i64 [ %30, %29 ], [ -1, %32 ]
ret i64 %35
}