#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__ strlen(char const *);
char *strstr(char const *, char const *);
int str_escape_get_len(const char *str, const char *endstr) {
  const char *ptr = str, *end;
  while ((end = strstr(ptr, endstr)) == NULL) {
    ptr += (strlen(ptr) + 1);
  }
  return end - str;
}


x86 assembly (clang, -O0):
<str_escape_get_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    116d <str_escape_get_len+0x4d>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1138 <str_escape_get_len+0x18>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_escape_get_len>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
call   1040 <strstr@plt>
test   %rax,%rax
jne    115f <str_escape_get_len+0x3f>
mov    %r14,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strstr@plt>
test   %rax,%rax
je     1140 <str_escape_get_len+0x20>
sub    %r14d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @str_escape_get_len(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
store i8* %7, i8** %5
br label %8
8: ; preds = %13, %2
%9 = load i8*, i8** %5
%10 = load i8*, i8** %4
%11 = call i8* @strstr(i8* %9, i8* %10)
store i8* %11, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %19
13: ; preds = %8
%14 = load i8*, i8** %5
%15 = call i64 @strlen(i8* %14)
%16 = add nsw i64 %15, 1
%17 = load i8*, i8** %5
%18 = getelementptr inbounds i8, i8* %17, i64 %16
store i8* %18, i8** %5
br label %8
19: ; preds = %8
%20 = load i8*, i8** %6
%21 = load i8*, i8** %3
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
%25 = trunc i64 %24 to i32
ret i32 %25
}

LLVM IR (-O2):
define i32 @str_escape_get_len(i8* %0, i8* nocapture %1) {
%3 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %12
5: ; preds = %2, %5
%6 = phi i8* [ %9, %5 ], [ %0, %2 ]
%7 = tail call i64 @strlen(i8* dereferenceable(1) %6)
%8 = add nsw i64 %7, 1
%9 = getelementptr inbounds i8, i8* %6, i64 %8
%10 = tail call i8* @strstr(i8* dereferenceable(1) %9, i8* dereferenceable(1) %1)
%11 = icmp eq i8* %10, null
br i1 %11, label %5, label %12
12: ; preds = %5, %2
%13 = phi i8* [ %3, %2 ], [ %10, %5 ]
%14 = ptrtoint i8* %13 to i64
%15 = ptrtoint i8* %0 to i64
%16 = sub i64 %14, %15
%17 = trunc i64 %16 to i32
ret i32 %17
}