#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 */
char const *memchr(char const *, char, int);
int /*<<< orphan*/ printf(char *);
int test_memchr(void) {
  const char *s = "";
  printf("\tmemchr");
  if (*(char *)memchr("", '\0', 1) != '\0')
    return 1;
  else
    printf(".");
  if (*(char *)memchr("123", '3', 3) != '3')
    return 1;
  else
    printf(".");
  if (memchr(s, 'a', 0) != s)
    return 1;
  else
    printf(".");
  if (memchr(s, 'a', 1) != &s[1])
    return 1;
  else
    printf(".");
  printf(" passed.\n");
  return 0;
}


x86 assembly (clang, -O0):
<test_memchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xee8(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xec6(%rip),%rdi
call   1030 <printf@plt>
lea    0xed1(%rip),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1040 <memchr@plt>
movsbl (%rax),%eax
cmp    $0x0,%eax
je     116a <test_memchr+0x4a>
movl   $0x1,-0x4(%rbp)
jmp    1231 <test_memchr+0x111>
lea    0xe97(%rip),%rdi
call   1030 <printf@plt>
lea    0xe8d(%rip),%rdi
mov    $0x33,%esi
mov    $0x3,%edx
call   1040 <memchr@plt>
movsbl (%rax),%eax
cmp    $0x33,%eax
je     11a4 <test_memchr+0x84>
movl   $0x1,-0x4(%rbp)
jmp    1231 <test_memchr+0x111>
lea    0xe5d(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x61,%esi
xor    %edx,%edx
call   1040 <memchr@plt>
cmp    -0x10(%rbp),%rax
je     11d6 <test_memchr+0xb6>
movl   $0x1,-0x4(%rbp)
jmp    1231 <test_memchr+0x111>
lea    0xe2b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x61,%esi
mov    $0x1,%edx
call   1040 <memchr@plt>
mov    -0x10(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
je     1212 <test_memchr+0xf2>
movl   $0x1,-0x4(%rbp)
jmp    1231 <test_memchr+0x111>
lea    0xdef(%rip),%rdi
call   1030 <printf@plt>
lea    0xde9(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_memchr>:
push   %r14
push   %rbx
push   %rax
lea    0xeb5(%rip),%rdi
call   1050 <printf@plt>
lea    0xebd(%rip),%r14
mov    $0x1,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1060 <memchr@plt>
cmpb   $0x0,(%rax)
jne    1200 <test_memchr+0xc0>
mov    $0x2e,%edi
call   1030 <putchar@plt>
lea    0xe83(%rip),%rdi
mov    $0x33,%esi
mov    $0x3,%edx
call   1060 <memchr@plt>
cmpb   $0x33,(%rax)
jne    1200 <test_memchr+0xc0>
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
mov    $0x61,%esi
xor    %edx,%edx
call   1060 <memchr@plt>
cmp    %r14,%rax
jne    1200 <test_memchr+0xc0>
mov    $0x2e,%edi
call   1030 <putchar@plt>
lea    0xe4c(%rip),%rdi
mov    $0x1,%ebx
mov    $0x61,%esi
mov    $0x1,%edx
call   1060 <memchr@plt>
lea    0xe32(%rip),%rcx
cmp    %rcx,%rax
jne    1200 <test_memchr+0xc0>
mov    $0x2e,%edi
call   1030 <putchar@plt>
lea    0xe13(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [8 x i8] c"\09memchr\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [4 x i8] c"123\00"
@.str.4 = [10 x i8] c" passed.\0A\00"
define i32 @test_memchr() {
%1 = alloca i32
%2 = alloca i8*
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %2
%3 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%4 = call i8* @memchr(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8 signext 0, i32 1)
%5 = load i8, i8* %4
%6 = sext i8 %5 to i32
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %0
store i32 1, i32* %1
br label %38
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %9
%12 = call i8* @memchr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8 signext 51, i32 3)
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 51
br i1 %15, label %16, label %17
16: ; preds = %11
store i32 1, i32* %1
br label %38
17: ; preds = %11
%18 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17
%20 = load i8*, i8** %2
%21 = call i8* @memchr(i8* %20, i8 signext 97, i32 0)
%22 = load i8*, i8** %2
%23 = icmp ne i8* %21, %22
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 1, i32* %1
br label %38
25: ; preds = %19
%26 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25
%28 = load i8*, i8** %2
%29 = call i8* @memchr(i8* %28, i8 signext 97, i32 1)
%30 = load i8*, i8** %2
%31 = getelementptr inbounds i8, i8* %30, i64 1
%32 = icmp ne i8* %29, %31
br i1 %32, label %33, label %34
33: ; preds = %27
store i32 1, i32* %1
br label %38
34: ; preds = %27
%35 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34
%37 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %38
38: ; preds = %36, %33, %24, %16, %8
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [8 x i8] c"\09memchr\00"
@.str.3 = [4 x i8] c"123\00"
@str = [9 x i8] c" passed.\00"
define i32 @test_memchr() {
%1 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call i8* @memchr(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8 signext 0, i32 1)
%3 = load i8, i8* %2
%4 = icmp eq i8 %3, 0
br i1 %4, label %5, label %21
5: ; preds = %0
%6 = tail call i32 @putchar(i32 46)
%7 = tail call i8* @memchr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8 signext 51, i32 3)
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 51
br i1 %9, label %10, label %21
10: ; preds = %5
%11 = tail call i32 @putchar(i32 46)
%12 = tail call i8* @memchr(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8 signext 97, i32 0)
%13 = icmp eq i8* %12, getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0)
br i1 %13, label %14, label %21
14: ; preds = %10
%15 = tail call i32 @putchar(i32 46)
%16 = tail call i8* @memchr(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8 signext 97, i32 1)
%17 = icmp eq i8* %16, getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 1, i64 0)
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = tail call i32 @putchar(i32 46)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %21
21: ; preds = %14, %10, %5, %0, %18
%22 = phi i32 [ 0, %18 ], [ 1, %0 ], [ 1, %5 ], [ 1, %10 ], [ 1, %14 ]
ret i32 %22
}