#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*/ __m128i;

/* Variables and functions */
int _SIDD_CMP_EQUAL_EACH;
int _SIDD_LEAST_SIGNIFICANT;
int _SIDD_UBYTE_OPS;
scalar_t__ _mm_cmpistrc(int /*<<< orphan*/ const, int /*<<< orphan*/ const,
                        int const);
int _mm_cmpistri(int /*<<< orphan*/ const, int /*<<< orphan*/ const, int const);
int /*<<< orphan*/ _mm_loadu_si128(int /*<<< orphan*/ *);
int /*<<< orphan*/ _mm_setzero_si128();
size_t strnlen_sse42(const char *str, size_t size) {
  size_t result = 0;
  __m128i *buf = (__m128i *)(const char *)(str);
  const __m128i zeros = _mm_setzero_si128();
  size--;
  while (size) {
    const __m128i data = _mm_loadu_si128(buf);
    const int mode =
        _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_EACH | _SIDD_LEAST_SIGNIFICANT;
    if (_mm_cmpistrc(data, zeros, mode)) {
      const int idx = _mm_cmpistri(data, zeros, mode);
      return result + idx;
    }
    buf++;
    result += 16;
    size--;
  }
  return 0;
}


x86 assembly (clang, -O0):
<strnlen_sse42>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <_mm_setzero_si128@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1211 <strnlen_sse42+0xd1>
mov    -0x28(%rbp),%rdi
call   1060 <_mm_loadu_si128@plt>
mov    %eax,-0x30(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    0x2e3b(%rip),%rcx
or     (%rcx),%eax
mov    0x2e42(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x34(%rbp),%edx
call   1040 <_mm_cmpistrc@plt>
cmp    $0x0,%rax
je     11e8 <strnlen_sse42+0xa8>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x34(%rbp),%edx
call   1050 <_mm_cmpistri@plt>
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x38(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    1219 <strnlen_sse42+0xd9>
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1176 <strnlen_sse42+0x36>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strnlen_sse42>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r13
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <_mm_setzero_si128@plt>
add    $0xffffffffffffffff,%r15
je     11ca <strnlen_sse42+0x8a>
mov    %eax,%r12d
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1060 <_mm_loadu_si128@plt>
mov    %eax,%ebx
mov    0x2e57(%rip),%rax
mov    (%rax),%ebp
mov    0x2e56(%rip),%rax
or     (%rax),%ebp
mov    0x2e55(%rip),%rax
or     (%rax),%ebp
mov    %ebx,%edi
mov    %r12d,%esi
mov    %ebp,%edx
call   1040 <_mm_cmpistrc@plt>
test   %rax,%rax
jne    11b8 <strnlen_sse42+0x78>
add    $0x4,%r13
add    $0xfffffffffffffff0,%r14
add    $0xffffffffffffffff,%r15
jne    1170 <strnlen_sse42+0x30>
xor    %ebx,%ebx
jmp    11ca <strnlen_sse42+0x8a>
mov    %ebx,%edi
mov    %r12d,%esi
mov    %ebp,%edx
call   1050 <_mm_cmpistri@plt>
movslq %eax,%rbx
sub    %r14,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@_SIDD_UBYTE_OPS = global i32 0
@_SIDD_CMP_EQUAL_EACH = global i32 0
@_SIDD_LEAST_SIGNIFICANT = global i32 0
define i64 @strnlen_sse42(i8* %0, i64 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 0, i64* %6
%12 = load i8*, i8** %4
%13 = bitcast i8* %12 to i32*
store i32* %13, i32** %7
%14 = call i32 (...) @_mm_setzero_si128()
store i32 %14, i32* %8
%15 = load i64, i64* %5
%16 = add i64 %15, -1
store i64 %16, i64* %5
br label %17
17: ; preds = %42, %2
%18 = load i64, i64* %5
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %49
20: ; preds = %17
%21 = load i32*, i32** %7
%22 = call i32 @_mm_loadu_si128(i32* %21)
store i32 %22, i32* %9
%23 = load i32, i32* @_SIDD_UBYTE_OPS
%24 = load i32, i32* @_SIDD_CMP_EQUAL_EACH
%25 = or i32 %23, %24
%26 = load i32, i32* @_SIDD_LEAST_SIGNIFICANT
%27 = or i32 %25, %26
store i32 %27, i32* %10
%28 = load i32, i32* %9
%29 = load i32, i32* %8
%30 = load i32, i32* %10
%31 = call i64 @_mm_cmpistrc(i32 %28, i32 %29, i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %42
33: ; preds = %20
%34 = load i32, i32* %9
%35 = load i32, i32* %8
%36 = load i32, i32* %10
%37 = call i32 @_mm_cmpistri(i32 %34, i32 %35, i32 %36)
store i32 %37, i32* %11
%38 = load i64, i64* %6
%39 = load i32, i32* %11
%40 = sext i32 %39 to i64
%41 = add i64 %38, %40
store i64 %41, i64* %3
br label %50
42: ; preds = %20
%43 = load i32*, i32** %7
%44 = getelementptr inbounds i32, i32* %43, i32 1
store i32* %44, i32** %7
%45 = load i64, i64* %6
%46 = add i64 %45, 16
store i64 %46, i64* %6
%47 = load i64, i64* %5
%48 = add i64 %47, -1
store i64 %48, i64* %5
br label %17
49: ; preds = %17
store i64 0, i64* %3
br label %50
50: ; preds = %49, %33
%51 = load i64, i64* %3
ret i64 %51
}

LLVM IR (-O2):
@_SIDD_UBYTE_OPS = global i32 0
@_SIDD_CMP_EQUAL_EACH = global i32 0
@_SIDD_LEAST_SIGNIFICANT = global i32 0
define i64 @strnlen_sse42(i8* %0, i64 %1) {
%3 = tail call i32 (...) @_mm_setzero_si128()
%4 = add i64 %1, -1
%5 = icmp eq i64 %4, 0
br i1 %5, label %29, label %6
6: ; preds = %2
%7 = bitcast i8* %0 to i32*
br label %8
8: ; preds = %6, %24
%9 = phi i64 [ %27, %24 ], [ %4, %6 ]
%10 = phi i64 [ %26, %24 ], [ 0, %6 ]
%11 = phi i32* [ %25, %24 ], [ %7, %6 ]
%12 = tail call i32 @_mm_loadu_si128(i32* %11)
%13 = load i32, i32* @_SIDD_UBYTE_OPS
%14 = load i32, i32* @_SIDD_CMP_EQUAL_EACH
%15 = or i32 %14, %13
%16 = load i32, i32* @_SIDD_LEAST_SIGNIFICANT
%17 = or i32 %15, %16
%18 = tail call i64 @_mm_cmpistrc(i32 %12, i32 %3, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %8
%21 = tail call i32 @_mm_cmpistri(i32 %12, i32 %3, i32 %17)
%22 = sext i32 %21 to i64
%23 = add i64 %10, %22
br label %29
24: ; preds = %8
%25 = getelementptr inbounds i32, i32* %11, i64 1
%26 = add i64 %10, 16
%27 = add i64 %9, -1
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %8
29: ; preds = %24, %2, %20
%30 = phi i64 [ %23, %20 ], [ 0, %2 ], [ 0, %24 ]
ret i64 %30
}