#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 strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int mystrnstr(char *str1, char *str2, int str1len) {
  int str2len, i;
  str2len = strlen(str2);
  if (str2len > str1len)
    return -1;
  for (i = 0; i <= str1len - str2len; i++)
    if (strncmp(str1 + i, str2, str2len) == 0)
      return i;
  return -1;
}


x86 assembly (clang, -O0):
<mystrnstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    1157 <mystrnstr+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b5 <mystrnstr+0x95>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    -0x20(%rbp),%ecx
cmp    %ecx,%eax
jg     11ae <mystrnstr+0x8e>
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    119b <mystrnstr+0x7b>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b5 <mystrnstr+0x95>
jmp    11a0 <mystrnstr+0x80>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    115e <mystrnstr+0x3e>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mystrnstr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r13d
mov    %rsi,%r12
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    $0xffffffff,%r14d
cmp    %r13d,%eax
jg     1181 <mystrnstr+0x61>
mov    %eax,%ebp
sub    %eax,%r13d
add    $0x1,%r13d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%r15,%rbx,1),%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     117e <mystrnstr+0x5e>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1160 <mystrnstr+0x40>
jmp    1181 <mystrnstr+0x61>
mov    %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 @mystrnstr(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = load i32, i32* %7
%14 = icmp sgt i32 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %39
16: ; preds = %3
store i32 0, i32* %9
br label %17
17: ; preds = %35, %16
%18 = load i32, i32* %9
%19 = load i32, i32* %7
%20 = load i32, i32* %8
%21 = sub nsw i32 %19, %20
%22 = icmp sle i32 %18, %21
br i1 %22, label %23, label %38
23: ; preds = %17
%24 = load i8*, i8** %5
%25 = load i32, i32* %9
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8*, i8** %6
%29 = load i32, i32* %8
%30 = call i64 @strncmp(i8* %27, i8* %28, i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %23
%33 = load i32, i32* %9
store i32 %33, i32* %4
br label %39
34: ; preds = %23
br label %35
35: ; preds = %34
%36 = load i32, i32* %9
%37 = add nsw i32 %36, 1
store i32 %37, i32* %9
br label %17
38: ; preds = %17
store i32 -1, i32* %4
br label %39
39: ; preds = %38, %32, %15
%40 = load i32, i32* %4
ret i32 %40
}

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