#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 count_lbx_entry_delimiter_chars(char *);
int count_lbx_key_chars(char *);
int count_lbx_key_value_delimiter_chars(char *);
int count_lbx_value_chars(char *);
int validate_lbx_entry(char *s) {
  int key_chars;
  int delimiter_chars;
  int entry_chars;
  int end_chars;
  int total_length;
  key_chars = count_lbx_key_chars(s);
  total_length = key_chars;
  delimiter_chars = count_lbx_key_value_delimiter_chars(s + total_length);
  total_length += delimiter_chars;
  entry_chars = count_lbx_value_chars(s + total_length);
  total_length += entry_chars;
  end_chars = count_lbx_entry_delimiter_chars(s + total_length);
  total_length += end_chars;
  if ((key_chars > 0) && (delimiter_chars > 0) && (entry_chars > 0) &&
      ((end_chars > 0) || (s[total_length] == 0))) {
    return total_length;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<validate_lbx_entry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <count_lbx_key_chars@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
call   1030 <count_lbx_key_value_delimiter_chars@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
call   1040 <count_lbx_value_chars@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
call   1060 <count_lbx_entry_delimiter_chars@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11fa <validate_lbx_entry+0xba>
cmpl   $0x0,-0x18(%rbp)
jle    11fa <validate_lbx_entry+0xba>
cmpl   $0x0,-0x1c(%rbp)
jle    11fa <validate_lbx_entry+0xba>
cmpl   $0x0,-0x20(%rbp)
jg     11ef <validate_lbx_entry+0xaf>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    11fa <validate_lbx_entry+0xba>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1201 <validate_lbx_entry+0xc1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validate_lbx_entry>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
call   1050 <count_lbx_key_chars@plt>
movslq %eax,%rbp
lea    (%r14,%rbp,1),%rdi
call   1030 <count_lbx_key_value_delimiter_chars@plt>
mov    %eax,%r15d
lea    (%r15,%rbp,1),%eax
movslq %eax,%rbx
lea    (%r14,%rbx,1),%rdi
call   1040 <count_lbx_value_chars@plt>
mov    %eax,%r12d
add    %eax,%ebx
movslq %ebx,%rdi
add    %r14,%rdi
call   1060 <count_lbx_entry_delimiter_chars@plt>
test   %ebp,%ebp
jle    119d <validate_lbx_entry+0x5d>
test   %r15d,%r15d
jle    119d <validate_lbx_entry+0x5d>
test   %r12d,%r12d
jle    119d <validate_lbx_entry+0x5d>
add    %eax,%ebx
test   %eax,%eax
jg     119f <validate_lbx_entry+0x5f>
movslq %ebx,%rax
cmpb   $0x0,(%r14,%rax,1)
je     119f <validate_lbx_entry+0x5f>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @validate_lbx_entry(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32 @count_lbx_key_chars(i8* %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
store i32 %11, i32* %8
%12 = load i8*, i8** %3
%13 = load i32, i32* %8
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = call i32 @count_lbx_key_value_delimiter_chars(i8* %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = load i32, i32* %8
%19 = add nsw i32 %18, %17
store i32 %19, i32* %8
%20 = load i8*, i8** %3
%21 = load i32, i32* %8
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = call i32 @count_lbx_value_chars(i8* %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = load i32, i32* %8
%27 = add nsw i32 %26, %25
store i32 %27, i32* %8
%28 = load i8*, i8** %3
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = call i32 @count_lbx_entry_delimiter_chars(i8* %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = load i32, i32* %8
%35 = add nsw i32 %34, %33
store i32 %35, i32* %8
%36 = load i32, i32* %4
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %57
38: ; preds = %1
%39 = load i32, i32* %5
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %57
41: ; preds = %38
%42 = load i32, i32* %6
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %57
44: ; preds = %41
%45 = load i32, i32* %7
%46 = icmp sgt i32 %45, 0
br i1 %46, label %55, label %47
47: ; preds = %44
%48 = load i8*, i8** %3
%49 = load i32, i32* %8
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %47, %44
%56 = load i32, i32* %8
store i32 %56, i32* %2
br label %58
57: ; preds = %47, %41, %38, %1
store i32 0, i32* %2
br label %58
58: ; preds = %57, %55
%59 = load i32, i32* %2
ret i32 %59
}

LLVM IR (-O2):
define i32 @validate_lbx_entry(i8* %0) {
%2 = tail call i32 @count_lbx_key_chars(i8* %0)
%3 = sext i32 %2 to i64
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = tail call i32 @count_lbx_key_value_delimiter_chars(i8* %4)
%6 = add nsw i32 %5, %2
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = tail call i32 @count_lbx_value_chars(i8* %8)
%10 = add nsw i32 %6, %9
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = tail call i32 @count_lbx_entry_delimiter_chars(i8* %12)
%14 = add nsw i32 %10, %13
%15 = icmp sgt i32 %2, 0
%16 = icmp sgt i32 %5, 0
%17 = select i1 %15, i1 %16, i1 false
%18 = icmp sgt i32 %9, 0
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %20, label %27
20: ; preds = %1
%21 = icmp sgt i32 %13, 0
br i1 %21, label %28, label %22
22: ; preds = %20
%23 = sext i32 %14 to i64
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %22, %1
br label %28
28: ; preds = %20, %22, %27
%29 = phi i32 [ 0, %27 ], [ %14, %22 ], [ %14, %20 ]
ret i32 %29
}