#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 scalar_t__ u_int32_t;

/* Variables and functions */
scalar_t__ u8_nextchar(char const *, size_t *);
char *u8_strchr(const char *s, u_int32_t ch, size_t *charn) {
  size_t i = 0, lasti = 0;
  u_int32_t c;
  *charn = 0;
  while (s[i]) {
    c = u8_nextchar(s, &i);
    if (c == ch) {
      return (char *)&s[lasti];
    }
    lasti = i;
    (*charn)++;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<u8_strchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     119c <u8_strchr+0x8c>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <u8_nextchar@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1181 <u8_strchr+0x71>
mov    -0x10(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a4 <u8_strchr+0x94>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
jmp    113f <u8_strchr+0x2f>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<u8_strchr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
movq   $0x0,(%rdx)
cmpb   $0x0,(%rdi)
je     1186 <u8_strchr+0x76>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
lea    0x8(%rsp),%rsi
call   1030 <u8_nextchar@plt>
cmp    %r15,%rax
jne    1151 <u8_strchr+0x41>
xor    %ebx,%ebx
add    %rbx,%r13
jmp    1189 <u8_strchr+0x79>
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
mov    0x8(%rsp),%rbx
addq   $0x1,(%r14)
cmpb   $0x0,0x0(%r13,%rbx,1)
je     1186 <u8_strchr+0x76>
mov    %r13,%rdi
mov    %r12,%rsi
call   1030 <u8_nextchar@plt>
cmp    %r15,%rax
jne    1160 <u8_strchr+0x50>
add    %rbx,%r13
jmp    1189 <u8_strchr+0x79>
xor    %r13d,%r13d
mov    %r13,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @u8_strchr(i8* %0, i64 %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
store i64 0, i64* %8
store i64 0, i64* %9
%11 = load i64*, i64** %7
store i64 0, i64* %11
br label %12
12: ; preds = %28, %3
%13 = load i8*, i8** %5
%14 = load i64, i64* %8
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = load i8, i8* %15
%17 = icmp ne i8 %16, 0
br i1 %17, label %18, label %33
18: ; preds = %12
%19 = load i8*, i8** %5
%20 = call i64 @u8_nextchar(i8* %19, i64* %8)
store i64 %20, i64* %10
%21 = load i64, i64* %10
%22 = load i64, i64* %6
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = load i8*, i8** %5
%26 = load i64, i64* %9
%27 = getelementptr inbounds i8, i8* %25, i64 %26
store i8* %27, i8** %4
br label %34
28: ; preds = %18
%29 = load i64, i64* %8
store i64 %29, i64* %9
%30 = load i64*, i64** %7
%31 = load i64, i64* %30
%32 = add i64 %31, 1
store i64 %32, i64* %30
br label %12
33: ; preds = %12
store i8* null, i8** %4
br label %34
34: ; preds = %33, %24
%35 = load i8*, i8** %4
ret i8* %35
}

LLVM IR (-O2):
define i8* @u8_strchr(i8* %0, i64 %1, i64* nocapture %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
store i64 0, i64* %4
store i64 0, i64* %2
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %24, label %8
8: ; preds = %3
%9 = call i64 @u8_nextchar(i8* %0, i64* %4)
%10 = icmp eq i64 %9, %1
br i1 %10, label %14, label %17
11: ; preds = %17
%12 = call i64 @u8_nextchar(i8* %0, i64* %4)
%13 = icmp eq i64 %12, %1
br i1 %13, label %14, label %17
14: ; preds = %11, %8
%15 = phi i64 [ 0, %8 ], [ %18, %11 ]
%16 = getelementptr inbounds i8, i8* %0, i64 %15
br label %24
17: ; preds = %8, %11
%18 = load i64, i64* %4
%19 = load i64, i64* %2
%20 = add i64 %19, 1
store i64 %20, i64* %2
%21 = getelementptr inbounds i8, i8* %0, i64 %18
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 0
br i1 %23, label %24, label %11
24: ; preds = %17, %3, %14
%25 = phi i8* [ %16, %14 ], [ null, %3 ], [ null, %17 ]
ret i8* %25
}