#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 */
size_t strlen(char const *);
const char *strchr(const char *str, int character) {
  for (size_t i = 0; i < strlen(str); i++) {
    if (str[i] == character) {
      return &str[i];
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<strchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
jae    1184 <strchr+0x74>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    -0x14(%rbp),%eax
jne    116e <strchr+0x5e>
mov    -0x10(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    118c <strchr+0x7c>
jmp    1173 <strchr+0x63>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1127 <strchr+0x17>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strchr>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %rax,%rax
je     1141 <strchr+0x31>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl (%rbx),%ecx
cmp    %ebp,%ecx
je     1143 <strchr+0x33>
add    $0x1,%rbx
add    $0xffffffffffffffff,%rax
jne    1130 <strchr+0x20>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @strchr(i8* %0, i32 %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %16, label %5
5: ; preds = %2, %11
%6 = phi i64 [ %12, %11 ], [ 0, %2 ]
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp eq i32 %9, %1
br i1 %10, label %14, label %11
11: ; preds = %5
%12 = add nuw i64 %6, 1
%13 = icmp eq i64 %12, %3
br i1 %13, label %16, label %5
14: ; preds = %5
%15 = getelementptr inbounds i8, i8* %0, i64 %6
br label %16
16: ; preds = %11, %14, %2
%17 = phi i8* [ null, %2 ], [ %15, %14 ], [ null, %11 ]
ret i8* %17
}