#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 */
int /*<<< orphan*/ printf(char *, ...);
char *strchr(char *, char);
void strchrTest() {
  char str[] = "This is a sample string";
  char *pch;
  printf("Looking for the 's' character in \"%s\"...\n", str);
  pch = strchr(str, 's');
  while (pch != NULL) {
    printf("found at %d\n", pch - str + 1);
    pch = strchr(pch + 1, 's');
  }
}


x86 assembly (clang, -O0):
<strchrTest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0xed1(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xece(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xecb(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    -0x20(%rbp),%rsi
lea    0xec4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x20(%rbp),%rdi
mov    $0x73,%esi
call   1030 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11b0 <strchrTest+0x90>
mov    -0x28(%rbp),%rsi
lea    -0x20(%rbp),%rax
sub    %rax,%rsi
add    $0x1,%rsi
lea    0xeb4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    $0x73,%esi
call   1030 <strchr@plt>
mov    %rax,-0x28(%rbp)
jmp    116d <strchrTest+0x4d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strchrTest>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
movaps 0xed0(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x676e6972747320,%rax
mov    %rax,0x10(%rsp)
lea    0xece(%rip),%rdi
mov    %rsp,%r14
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x73,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     11a7 <strchrTest+0x87>
mov    %rax,%rbx
mov    $0x1,%r15d
sub    %r14,%r15
lea    0xec6(%rip),%r14
nopl   0x0(%rax)
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x73,%esi
call   1030 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1180 <strchrTest+0x60>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.strchrTest.str = [24 x i8] c"This is a sample string\00"
@.str = [42 x i8] c"Looking for the 's' character in \22%s\22...\0A\00"
@.str.1 = [13 x i8] c"found at %d\0A\00"
define void @strchrTest() {
%1 = alloca [24 x i8]
%2 = alloca i8*
%3 = bitcast [24 x i8]* %1 to i8*
%4 = getelementptr inbounds [24 x i8], [24 x i8]* %1, i64 0, i64 0
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %4)
%6 = getelementptr inbounds [24 x i8], [24 x i8]* %1, i64 0, i64 0
%7 = call i8* @strchr(i8* %6, i8 signext 115)
store i8* %7, i8** %2
br label %8
8: ; preds = %11, %0
%9 = load i8*, i8** %2
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %22
11: ; preds = %8
%12 = load i8*, i8** %2
%13 = getelementptr inbounds [24 x i8], [24 x i8]* %1, 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 ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i64 %17)
%19 = load i8*, i8** %2
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = call i8* @strchr(i8* %20, i8 signext 115)
store i8* %21, i8** %2
br label %8
22: ; preds = %8
ret void
}

LLVM IR (-O2):
@__const.strchrTest.str = [24 x i8] c"This is a sample string\00"
@.str = [42 x i8] c"Looking for the 's' character in \22%s\22...\0A\00"
@.str.1 = [13 x i8] c"found at %d\0A\00"
define void @strchrTest() {
%1 = alloca [24 x i8]
%2 = getelementptr inbounds [24 x i8], [24 x i8]* %1, i64 0, i64 0
%3 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %2)
%4 = call i8* @strchr(i8* %2, i8 signext 115)
%5 = icmp eq i8* %4, null
br i1 %5, label %17, label %6
6: ; preds = %0
%7 = ptrtoint [24 x i8]* %1 to i64
%8 = sub i64 1, %7
br label %9
9: ; preds = %6, %9
%10 = phi i8* [ %4, %6 ], [ %15, %9 ]
%11 = ptrtoint i8* %10 to i64
%12 = add i64 %8, %11
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i64 %12)
%14 = getelementptr inbounds i8, i8* %10, i64 1
%15 = call i8* @strchr(i8* %14, i8 signext 115)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %9
17: ; preds = %9, %0
ret void
}