#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 */
scalar_t__ memcmp(unsigned char const *, unsigned char const *,
                  unsigned int const);
unsigned int pos_in_mem(const unsigned char *haystack,
                        const unsigned int haystack_size,
                        const unsigned char *needle,
                        const unsigned int needle_size) {
  unsigned int i;
  if (haystack_size < needle_size)
    return 0;
  for (i = 0; i <= haystack_size - needle_size; i++)
    if (memcmp(&haystack[i], needle, needle_size) == 0)
      return (i + needle_size);
  return 0;
}


x86 assembly (clang, -O0):
<pos_in_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
jae    113e <pos_in_mem+0x2e>
movl   $0x0,-0x4(%rbp)
jmp    119e <pos_in_mem+0x8e>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    -0x24(%rbp),%ecx
cmp    %ecx,%eax
ja     1197 <pos_in_mem+0x87>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%eax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <memcmp@plt>
cmp    $0x0,%rax
jne    1184 <pos_in_mem+0x74>
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119e <pos_in_mem+0x8e>
jmp    1189 <pos_in_mem+0x79>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1145 <pos_in_mem+0x35>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pos_in_mem>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebp
xor    %eax,%eax
sub    %ecx,%ebp
jb     1156 <pos_in_mem+0x46>
mov    %ecx,%r12d
mov    %rdx,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
add    %r15,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1030 <memcmp@plt>
test   %rax,%rax
je     1150 <pos_in_mem+0x40>
add    $0x1,%ebx
cmp    %ebp,%ebx
jbe    1130 <pos_in_mem+0x20>
xor    %eax,%eax
jmp    1156 <pos_in_mem+0x46>
add    %ebx,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @pos_in_mem(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
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = load i32, i32* %7
%12 = load i32, i32* %9
%13 = icmp ult i32 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %4
store i32 0, i32* %5
br label %40
15: ; preds = %4
store i32 0, i32* %10
br label %16
16: ; preds = %36, %15
%17 = load i32, i32* %10
%18 = load i32, i32* %7
%19 = load i32, i32* %9
%20 = sub i32 %18, %19
%21 = icmp ule i32 %17, %20
br i1 %21, label %22, label %39
22: ; preds = %16
%23 = load i8*, i8** %6
%24 = load i32, i32* %10
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8*, i8** %8
%28 = load i32, i32* %9
%29 = call i64 @memcmp(i8* %26, i8* %27, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %22
%32 = load i32, i32* %10
%33 = load i32, i32* %9
%34 = add i32 %32, %33
store i32 %34, i32* %5
br label %40
35: ; preds = %22
br label %36
36: ; preds = %35
%37 = load i32, i32* %10
%38 = add i32 %37, 1
store i32 %38, i32* %10
br label %16
39: ; preds = %16
store i32 0, i32* %5
br label %40
40: ; preds = %39, %31, %14
%41 = load i32, i32* %5
ret i32 %41
}

LLVM IR (-O2):
define i32 @pos_in_mem(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = icmp ult i32 %1, %3
br i1 %5, label %19, label %6
6: ; preds = %4
%7 = sub i32 %1, %3
br label %8
8: ; preds = %6, %16
%9 = phi i32 [ 0, %6 ], [ %17, %16 ]
%10 = zext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = tail call i64 @memcmp(i8* %11, i8* %2, i32 %3)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = add i32 %9, %3
br label %19
16: ; preds = %8
%17 = add i32 %9, 1
%18 = icmp ugt i32 %17, %7
br i1 %18, label %19, label %8
19: ; preds = %16, %4, %14
%20 = phi i32 [ %15, %14 ], [ 0, %4 ], [ 0, %16 ]
ret i32 %20
}