#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 */
scalar_t__ memchr(char *, char, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strlen(char *);
void memchrTest() {
  char *pch;
  char str[] = "Examle string";
  pch = (char *)memchr(str, 'p', strlen(str));
  if (pch != NULL) {
    printf("'p' found at position %d.\n", pch - str + 1);
  } else {
    printf("'p' not found.\n");
  }
}


x86 assembly (clang, -O0):
<memchrTest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0xec1(%rip),%rax
mov    %rax,-0x16(%rbp)
mov    0xebf(%rip),%eax
mov    %eax,-0xe(%rbp)
mov    0xeb9(%rip),%ax
mov    %ax,-0xa(%rbp)
lea    -0x16(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x16(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%edx
mov    $0x70,%esi
call   1050 <memchr@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11a9 <memchrTest+0x79>
mov    -0x8(%rbp),%rsi
lea    -0x16(%rbp),%rax
sub    %rax,%rsi
add    $0x1,%rsi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11b7 <memchrTest+0x87>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memchrTest>:
push   %rbx
sub    $0x10,%rsp
movabs $0x676e6972747320,%rax
mov    %rax,0x6(%rsp)
movabs $0x7320656c6d617845,%rax
65 20 73
mov    %rax,(%rsp)
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    $0x70,%esi
mov    %eax,%edx
call   1060 <memchr@plt>
test   %rax,%rax
je     119b <memchrTest+0x5b>
sub    %rbx,%rax
add    $0x1,%rax
lea    0xe7f(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11a7 <memchrTest+0x67>
lea    0xe87(%rip),%rdi
call   1030 <puts@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@__const.memchrTest.str = [14 x i8] c"Examle string\00"
@.str = [27 x i8] c"'p' found at position %d.\0A\00"
@.str.1 = [16 x i8] c"'p' not found.\0A\00"
define void @memchrTest() {
%1 = alloca i8*
%2 = alloca [14 x i8]
%3 = bitcast [14 x i8]* %2 to i8*
%4 = getelementptr inbounds [14 x i8], [14 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [14 x i8], [14 x i8]* %2, i64 0, i64 0
%6 = call i32 @strlen(i8* %5)
%7 = call i64 @memchr(i8* %4, i8 signext 112, i32 %6)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %1
%9 = load i8*, i8** %1
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %19
11: ; preds = %0
%12 = load i8*, i8** %1
%13 = getelementptr inbounds [14 x i8], [14 x i8]* %2, i64 0, i64 0
%14 = ptrtoint i8* %12 to i64
%15 = ptrtoint i8* %13 to i64
%16 = sub i64 %14, %15
%17 = add nsw i64 %16, 1
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i64 %17)
br label %21
19: ; preds = %0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %11
ret void
}

LLVM IR (-O2):
@__const.memchrTest.str = [14 x i8] c"Examle string\00"
@.str = [27 x i8] c"'p' found at position %d.\0A\00"
@str = [15 x i8] c"'p' not found.\00"
define void @memchrTest() {
%1 = alloca [14 x i8]
%2 = getelementptr inbounds [14 x i8], [14 x i8]* %1, i64 0, i64 0
%3 = call i32 @strlen(i8* %2)
%4 = call i64 @memchr(i8* %2, i8 signext 112, i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %11, label %6
6: ; preds = %0
%7 = ptrtoint [14 x i8]* %1 to i64
%8 = sub i64 1, %7
%9 = add i64 %8, %4
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i64 %9)
br label %13
11: ; preds = %0
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %13
13: ; preds = %11, %6
ret void
}