#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*/ vassert(int /*<<< orphan*/, char *);
char *strchr(const char *str, int ch) {
  vassert(str != NULL, "str == NULL");
  while (*str) {
    if (*str++ == ch)
      return (char *)(str - 1);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<strchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
lea    0xecd(%rip),%rsi
call   1030 <vassert@plt>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1176 <strchr+0x66>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%eax
cmp    -0x14(%rbp),%eax
jne    1171 <strchr+0x61>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    117e <strchr+0x6e>
jmp    1138 <strchr+0x28>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strchr>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
lea    0xed8(%rip),%rsi
call   1030 <vassert@plt>
add    $0xffffffffffffffff,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl 0x1(%rbx),%eax
test   %eax,%eax
je     1152 <strchr+0x42>
add    $0x1,%rbx
cmp    %ebp,%eax
jne    1140 <strchr+0x30>
jmp    1154 <strchr+0x44>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"str == NULL\00"
define i8* @strchr(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%6 = load i8*, i8** %4
%7 = icmp ne i8* %6, null
%8 = zext i1 %7 to i32
%9 = call i32 @vassert(i32 %8, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %24, %2
%11 = load i8*, i8** %4
%12 = load i8, i8* %11
%13 = icmp ne i8 %12, 0
br i1 %13, label %14, label %25
14: ; preds = %10
%15 = load i8*, i8** %4
%16 = getelementptr inbounds i8, i8* %15, i32 1
store i8* %16, i8** %4
%17 = load i8, i8* %15
%18 = sext i8 %17 to i32
%19 = load i32, i32* %5
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i64 -1
store i8* %23, i8** %3
br label %26
24: ; preds = %14
br label %10
25: ; preds = %10
store i8* null, i8** %3
br label %26
26: ; preds = %25, %21
%27 = load i8*, i8** %3
ret i8* %27
}

LLVM IR (-O2):
@.str = [12 x i8] c"str == NULL\00"
define i8* @strchr(i8* %0, i32 %1) {
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @vassert(i32 %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %10, %2
%7 = phi i8* [ %0, %2 ], [ %11, %10 ]
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = getelementptr inbounds i8, i8* %7, i64 1
%12 = sext i8 %8 to i32
%13 = icmp eq i32 %12, %1
br i1 %13, label %14, label %6
14: ; preds = %6, %10
%15 = phi i8* [ %7, %10 ], [ null, %6 ]
ret i8* %15
}