#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 validate_lbx_entry(char *);
int validate_lbx_items(char *s) {
  int total_length;
  int entry_length;
  int n;
  total_length = 0;
  n = 0;
  do {
    entry_length = validate_lbx_entry(s + total_length);
    total_length += entry_length;
    if (entry_length > 0)
      n++;
  } while (entry_length > 0);
  if (s[total_length] == 0) {
    return n;
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<validate_lbx_items>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
call   1030 <validate_lbx_entry@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1159 <validate_lbx_items+0x49>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    115e <validate_lbx_items+0x4e>
cmpl   $0x0,-0x18(%rbp)
jg     112a <validate_lbx_items+0x1a>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1188 <validate_lbx_items+0x78>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118f <validate_lbx_items+0x7f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validate_lbx_items>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %ebx,%ebx
mov    $0xffffffff,%ebp
xchg   %ax,%ax
movslq %ebx,%rbx
lea    (%r14,%rbx,1),%rdi
call   1030 <validate_lbx_entry@plt>
add    %eax,%ebx
add    $0x1,%ebp
test   %eax,%eax
jg     1120 <validate_lbx_items+0x10>
movslq %ebx,%rax
xor    %ecx,%ecx
cmp    (%r14,%rax,1),%cl
sbb    %eax,%eax
or     %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @validate_lbx_items(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
store i32 0, i32* %6
br label %7
7: ; preds = %22, %1
%8 = load i8*, i8** %3
%9 = load i32, i32* %4
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %8, i64 %10
%12 = call i32 @validate_lbx_entry(i8* %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = load i32, i32* %4
%15 = add nsw i32 %14, %13
store i32 %15, i32* %4
%16 = load i32, i32* %5
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %7
%19 = load i32, i32* %6
%20 = add nsw i32 %19, 1
store i32 %20, i32* %6
br label %21
21: ; preds = %18, %7
br label %22
22: ; preds = %21
%23 = load i32, i32* %5
%24 = icmp sgt i32 %23, 0
br i1 %24, label %7, label %25
25: ; preds = %22
%26 = load i8*, i8** %3
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = load i32, i32* %6
store i32 %34, i32* %2
br label %36
35: ; preds = %25
store i32 -1, i32* %2
br label %36
36: ; preds = %35, %33
%37 = load i32, i32* %2
ret i32 %37
}

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