#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*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
scalar_t__ strchr(char *, char);
size_t b_string_strchr(void *dummy) {
  char *buf = (char *)malloc(500000);
  size_t i;
  size_t cs;
  memset(buf, 'a', 500000);
  buf[500000 - 1] = 0;
  buf[500000 - 2] = 'b';
  for (i = 0; i < 100; i++) {
    buf[i] = '0' + i % 8;
    cs += (int)strchr(buf, 'b');
  }
  free(buf);
  return cs;
}


x86 assembly (clang, -O0):
<b_string_strchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x7a120,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x61,%esi
mov    $0x7a120,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,0x7a11f(%rax)
mov    -0x10(%rbp),%rax
movb   $0x62,0x7a11e(%rax)
movq   $0x0,-0x18(%rbp)
cmpq   $0x64,-0x18(%rbp)
jae    11d8 <b_string_strchr+0x98>
mov    -0x18(%rbp),%rax
and    $0x7,%rax
add    $0x30,%rax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    $0x62,%esi
call   1040 <strchr@plt>
cltq
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    118b <b_string_strchr+0x4b>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<b_string_strchr>:
push   %r15
push   %r14
push   %rbx
mov    $0x7a120,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x61,%esi
mov    $0x7a120,%edx
call   1050 <memset@plt>
movw   $0x62,0x7a11e(%r14)
xor    %ebx,%ebx
mov    %ebx,%eax
and    $0x7,%al
or     $0x30,%al
mov    %al,(%r14,%rbx,1)
mov    %r14,%rdi
mov    $0x62,%esi
call   1040 <strchr@plt>
cltq
add    %rax,%r15
add    $0x1,%rbx
cmp    $0x64,%rbx
jne    1170 <b_string_strchr+0x30>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @b_string_strchr(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
store i8* %0, i8** %2
%6 = call i64 @malloc(i32 500000)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @memset(i8* %8, i8 signext 97, i32 500000)
%10 = load i8*, i8** %3
%11 = getelementptr inbounds i8, i8* %10, i64 499999
store i8 0, i8* %11
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i64 499998
store i8 98, i8* %13
store i64 0, i64* %4
br label %14
14: ; preds = %31, %1
%15 = load i64, i64* %4
%16 = icmp ult i64 %15, 100
br i1 %16, label %17, label %34
17: ; preds = %14
%18 = load i64, i64* %4
%19 = urem i64 %18, 8
%20 = add i64 48, %19
%21 = trunc i64 %20 to i8
%22 = load i8*, i8** %3
%23 = load i64, i64* %4
%24 = getelementptr inbounds i8, i8* %22, i64 %23
store i8 %21, i8* %24
%25 = load i8*, i8** %3
%26 = call i64 @strchr(i8* %25, i8 signext 98)
%27 = trunc i64 %26 to i32
%28 = sext i32 %27 to i64
%29 = load i64, i64* %5
%30 = add i64 %29, %28
store i64 %30, i64* %5
br label %31
31: ; preds = %17
%32 = load i64, i64* %4
%33 = add i64 %32, 1
store i64 %33, i64* %4
br label %14
34: ; preds = %14
%35 = load i8*, i8** %3
%36 = call i32 @free(i8* %35)
%37 = load i64, i64* %5
ret i64 %37
}

LLVM IR (-O2):
define i64 @b_string_strchr(i8* nocapture readnone %0) {
%2 = tail call i64 @malloc(i32 500000)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i32 @memset(i8* %3, i8 signext 97, i32 500000)
%5 = getelementptr inbounds i8, i8* %3, i64 499999
store i8 0, i8* %5
%6 = getelementptr inbounds i8, i8* %3, i64 499998
store i8 98, i8* %6
br label %7
7: ; preds = %1, %7
%8 = phi i64 [ undef, %1 ], [ %17, %7 ]
%9 = phi i64 [ 0, %1 ], [ %18, %7 ]
%10 = trunc i64 %9 to i8
%11 = and i8 %10, 7
%12 = or i8 %11, 48
%13 = getelementptr inbounds i8, i8* %3, i64 %9
store i8 %12, i8* %13
%14 = tail call i64 @strchr(i8* %3, i8 signext 98)
%15 = shl i64 %14, 32
%16 = ashr exact i64 %15, 32
%17 = add i64 %16, %8
%18 = add nuw nsw i64 %9, 1
%19 = icmp eq i64 %18, 100
br i1 %19, label %20, label %7
20: ; preds = %7
%21 = tail call i32 @free(i8* %3)
ret i64 %17
}