#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 strlen(char *);
char *find(char *haystack, char needle) {
  int i = 0;
  for (i = 0; i < strlen(haystack); i++) {
    if (i == strlen(haystack)) {
      return NULL;
    }
    if (needle == haystack[i]) {
      return (haystack + i);
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<find>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
jge    11b4 <find+0xa4>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jne    1175 <find+0x65>
movq   $0x0,-0x8(%rbp)
jmp    11bc <find+0xac>
movsbl -0x11(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x18(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    11a1 <find+0x91>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11bc <find+0xac>
jmp    11a6 <find+0x96>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1130 <find+0x20>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%rbx
call   1030 <strlen@plt>
xor    %r14d,%r14d
test   %eax,%eax
jle    1160 <find+0x50>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%rbp
je     1160 <find+0x50>
cmp    %r15b,(%rbx,%rbp,1)
je     115a <find+0x4a>
add    $0x1,%rbp
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbp
jl     1130 <find+0x20>
jmp    1160 <find+0x50>
add    %rbp,%rbx
mov    %rbx,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @find(i8* %0, i8 signext %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %22
5: ; preds = %2, %14
%6 = phi i64 [ %15, %14 ], [ 0, %2 ]
%7 = tail call i32 @strlen(i8* %0)
%8 = zext i32 %7 to i64
%9 = icmp eq i64 %6, %8
br i1 %9, label %22, label %10
10: ; preds = %5
%11 = getelementptr inbounds i8, i8* %0, i64 %6
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, %1
br i1 %13, label %19, label %14
14: ; preds = %10
%15 = add nuw nsw i64 %6, 1
%16 = tail call i32 @strlen(i8* %0)
%17 = sext i32 %16 to i64
%18 = icmp slt i64 %15, %17
br i1 %18, label %5, label %22
19: ; preds = %10
%20 = and i64 %6, 4294967295
%21 = getelementptr inbounds i8, i8* %0, i64 %20
br label %22
22: ; preds = %5, %14, %2, %19
%23 = phi i8* [ %21, %19 ], [ null, %2 ], [ null, %14 ], [ null, %5 ]
ret i8* %23
}