#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;
  for (i = 0; i < haystack_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)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    117a <pos_in_mem+0x6a>
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    1167 <pos_in_mem+0x57>
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1181 <pos_in_mem+0x71>
jmp    116c <pos_in_mem+0x5c>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    112d <pos_in_mem+0x1d>
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   %r13
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
je     1160 <pos_in_mem+0x50>
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rdi,%rbp
mov    %ecx,%r12d
mov    %esi,%r13d
neg    %r13
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1030 <memcmp@plt>
test   %rax,%rax
je     1165 <pos_in_mem+0x55>
add    $0xffffffffffffffff,%rbx
add    $0x1,%rbp
cmp    %rbx,%r13
jne    1140 <pos_in_mem+0x30>
xor    %r14d,%r14d
jmp    1168 <pos_in_mem+0x58>
sub    %ebx,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
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
store i32 0, i32* %10
br label %11
11: ; preds = %29, %4
%12 = load i32, i32* %10
%13 = load i32, i32* %7
%14 = icmp ult i32 %12, %13
br i1 %14, label %15, label %32
15: ; preds = %11
%16 = load i8*, i8** %6
%17 = load i32, i32* %10
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8*, i8** %8
%21 = load i32, i32* %9
%22 = call i64 @memcmp(i8* %19, i8* %20, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %15
%25 = load i32, i32* %10
%26 = load i32, i32* %9
%27 = add i32 %25, %26
store i32 %27, i32* %5
br label %33
28: ; preds = %15
br label %29
29: ; preds = %28
%30 = load i32, i32* %10
%31 = add i32 %30, 1
store i32 %31, i32* %10
br label %11
32: ; preds = %11
store i32 0, i32* %5
br label %33
33: ; preds = %32, %24
%34 = load i32, i32* %5
ret i32 %34
}

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