#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__ pos_gt_lim(char const *, char const *);
int safe_str_len(const char *start, const char *lim) {
  unsigned cnt = 0;
  while (1) {
    if (pos_gt_lim(start + 1, lim)) {
      cnt = -1;
      break;
    }
    if (0 == *start) {
      break;
    }
    ++cnt;
    ++start;
  }
  return cnt;
}


x86 assembly (clang, -O0):
<safe_str_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <pos_gt_lim@plt>
cmp    $0x0,%rax
je     114e <safe_str_len+0x3e>
movl   $0xffffffff,-0x14(%rbp)
jmp    117e <safe_str_len+0x6e>
mov    -0x8(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    1164 <safe_str_len+0x54>
jmp    117e <safe_str_len+0x6e>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1127 <safe_str_len+0x17>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_str_len>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rbp
add    $0x1,%rdi
call   1030 <pos_gt_lim@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
jne    1162 <safe_str_len+0x52>
add    $0x2,%rbp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
cmpb   $0x0,-0x2(%rbp)
je     115f <safe_str_len+0x4f>
add    $0x1,%ebx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1030 <pos_gt_lim@plt>
add    $0x1,%rbp
test   %rax,%rax
je     1140 <safe_str_len+0x30>
jmp    1162 <safe_str_len+0x52>
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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