#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 */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
scalar_t__ memcmp(int /*<<< orphan*/ *, int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, size_t);
unsigned int utilities_null_terminated_length(void *array,
                                              size_t element_size) {
  unsigned int ret = 0;
  uint8_t comparrison[element_size];
  memset(comparrison, 0, element_size);
  while (memcmp(&((uint8_t *)array)[ret * element_size], comparrison,
                element_size))
    ret++;
  return ret;
}


x86 assembly (clang, -O0):
<utilities_null_terminated_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%eax
imul   -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rdx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
je     11a2 <utilities_null_terminated_length+0x82>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    116a <utilities_null_terminated_length+0x4a>
mov    -0x14(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utilities_null_terminated_length>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsp,%r15
lea    0xf(,%rsi,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,%rdi
xor    %esi,%esi
mov    %r12,%rdx
call   1030 <memset@plt>
mov    $0xffffffff,%ebx
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebx
mov    %rbx,%rax
imul   %r12,%rax
lea    (%r14,%rax,4),%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <memcmp@plt>
test   %rax,%rax
jne    1160 <utilities_null_terminated_length+0x40>
mov    %ebx,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @utilities_null_terminated_length(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
store i64 %1, i64* %4
store i32 0, i32* %5
%8 = load i64, i64* %4
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %6
%10 = alloca i32, i64 %8
store i64 %8, i64* %7
%11 = load i64, i64* %4
%12 = call i32 @memset(i32* %10, i32 0, i64 %11)
br label %13
13: ; preds = %24, %2
%14 = load i8*, i8** %3
%15 = bitcast i8* %14 to i32*
%16 = load i32, i32* %5
%17 = zext i32 %16 to i64
%18 = load i64, i64* %4
%19 = mul i64 %17, %18
%20 = getelementptr inbounds i32, i32* %15, i64 %19
%21 = load i64, i64* %4
%22 = call i64 @memcmp(i32* %20, i32* %10, i64 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %13
%25 = load i32, i32* %5
%26 = add i32 %25, 1
store i32 %26, i32* %5
br label %13
27: ; preds = %13
%28 = load i32, i32* %5
%29 = load i8*, i8** %6
ret i32 %28
}

LLVM IR (-O2):
define i32 @utilities_null_terminated_length(i8* %0, i64 %1) {
%3 = alloca i32, i64 %1
%4 = call i32 @memset(i32* %3, i32 0, i64 %1)
%5 = bitcast i8* %0 to i32*
br label %6
6: ; preds = %6, %2
%7 = phi i32 [ 0, %2 ], [ %13, %6 ]
%8 = zext i32 %7 to i64
%9 = mul i64 %8, %1
%10 = getelementptr inbounds i32, i32* %5, i64 %9
%11 = call i64 @memcmp(i32* %10, i32* %3, i64 %1)
%12 = icmp eq i64 %11, 0
%13 = add i32 %7, 1
br i1 %12, label %14, label %6
14: ; preds = %6
ret i32 %7
}