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

/* Variables and functions */
int /*<<< orphan*/ *memchr(char const *, char const, int);
int /*<<< orphan*/ printf(char *, char const *, char *);
scalar_t__ strcmp(void *, char const *);
int _memchr(char const *progname) {
  int ret = 0;
  char const search[] = "sear\xff\xfe\x7f\x7e\x00\x01ch";
  size_t i;
  void *p;
  printf("%s: Testing %s\n", progname, "memchr()");
  for (i = 0; i < sizeof(search); i++) {
    p = memchr(search, search[i], sizeof(search));
    if (p == NULL || strcmp(p, &search[i]) != 0)
      ret += 1;
  }
  ret += memchr(search, 'z', sizeof(search)) == NULL ? 0 : 1;
  return ret;
}


x86 assembly (clang, -O0):
<_memchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0xeb6(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xeb4(%rip),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xeaa(%rip),%rdi
lea    0xeb3(%rip),%rdx
call   1030 <printf@plt>
movq   $0x0,-0x20(%rbp)
cmpq   $0xc,-0x20(%rbp)
jae    11e3 <_memchr+0xb3>
lea    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp,%rax,1),%al
movsbl %al,%esi
mov    $0xc,%edx
call   1040 <memchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11c4 <_memchr+0x94>
mov    -0x28(%rbp),%rdi
lea    -0x18(%rbp),%rsi
add    -0x20(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11cd <_memchr+0x9d>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11d2 <_memchr+0xa2>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1176 <_memchr+0x46>
lea    -0x18(%rbp),%rdi
mov    $0x7a,%esi
mov    $0xc,%edx
call   1040 <memchr@plt>
mov    %rax,%rdx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmove  %ecx,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_memchr>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rsi
movabs $0x7e7ffeff72616573,%rax
fe 7f 7e
mov    %rax,(%rsp)
movl   $0x681c00,0x8(%rsp)
lea    0xeb3(%rip),%rdi
lea    0xebc(%rip),%rdx
call   1030 <printf@plt>
xor    %r15d,%r15d
mov    $0x73,%al
mov    %rsp,%r14
xor    %ebx,%ebx
nop
movsbl %al,%esi
mov    %r14,%rdi
mov    $0xc,%edx
call   1040 <memchr@plt>
test   %rax,%rax
je     1196 <_memchr+0x66>
lea    (%rsp,%rbx,1),%rsi
mov    %rax,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     119a <_memchr+0x6a>
add    $0x1,%r15d
cmp    $0xb,%rbx
je     11ab <_memchr+0x7b>
movzbl 0x1(%rsp,%rbx,1),%eax
add    $0x1,%rbx
jmp    1170 <_memchr+0x40>
mov    %rsp,%rdi
mov    $0x7a,%esi
mov    $0xc,%edx
call   1040 <memchr@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const._memchr.search = [12 x i8] c"sear\FF\FE\7F~\00\1Ch\00"
@.str = [16 x i8] c"%s: Testing %s\0A\00"
@.str.1 = [9 x i8] c"memchr()\00"
define i32 @_memchr(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [12 x i8]
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %2
store i32 0, i32* %3
%7 = bitcast [12 x i8]* %4 to i8*
%8 = load i8*, i8** %2
%9 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %8, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i64 0, i64* %5
br label %10
10: ; preds = %32, %1
%11 = load i64, i64* %5
%12 = icmp ult i64 %11, 12
br i1 %12, label %13, label %35
13: ; preds = %10
%14 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%15 = load i64, i64* %5
%16 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 %15
%17 = load i8, i8* %16
%18 = call i32* @memchr(i8* %14, i8 signext %17, i32 12)
%19 = bitcast i32* %18 to i8*
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = icmp eq i8* %20, null
br i1 %21, label %28, label %22
22: ; preds = %13
%23 = load i8*, i8** %6
%24 = load i64, i64* %5
%25 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 %24
%26 = call i64 @strcmp(i8* %23, i8* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %22, %13
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
store i32 %30, i32* %3
br label %31
31: ; preds = %28, %22
br label %32
32: ; preds = %31
%33 = load i64, i64* %5
%34 = add i64 %33, 1
store i64 %34, i64* %5
br label %10
35: ; preds = %10
%36 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%37 = call i32* @memchr(i8* %36, i8 signext 122, i32 12)
%38 = icmp eq i32* %37, null
%39 = zext i1 %38 to i64
%40 = select i1 %38, i32 0, i32 1
%41 = load i32, i32* %3
%42 = add nsw i32 %41, %40
store i32 %42, i32* %3
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@__const._memchr.search = [12 x i8] c"sear\FF\FE\7F~\00\1Ch\00"
@.str = [16 x i8] c"%s: Testing %s\0A\00"
@.str.1 = [9 x i8] c"memchr()\00"
define i32 @_memchr(i8* %0) {
%2 = alloca [12 x i8]
%3 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %5
5: ; preds = %22, %1
%6 = phi i8 [ 115, %1 ], [ %24, %22 ]
%7 = phi i32 [ 0, %1 ], [ %19, %22 ]
%8 = phi i64 [ 0, %1 ], [ %20, %22 ]
%9 = call i32* @memchr(i8* %3, i8 signext %6, i32 12)
%10 = icmp eq i32* %9, null
br i1 %10, label %16, label %11
11: ; preds = %5
%12 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 %8
%13 = bitcast i32* %9 to i8*
%14 = call i64 @strcmp(i8* %13, i8* %12)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %11, %5
%17 = add nsw i32 %7, 1
br label %18
18: ; preds = %11, %16
%19 = phi i32 [ %17, %16 ], [ %7, %11 ]
%20 = add nuw nsw i64 %8, 1
%21 = icmp eq i64 %20, 12
br i1 %21, label %25, label %22
22: ; preds = %18
%23 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 %20
%24 = load i8, i8* %23
br label %5
25: ; preds = %18
%26 = call i32* @memchr(i8* %3, i8 signext 122, i32 12)
%27 = icmp ne i32* %26, null
%28 = zext i1 %27 to i32
%29 = add nsw i32 %19, %28
ret i32 %29
}