#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;
  for (i = 0; i <= strlen(haystack); i++) {
    if (i == strlen(haystack)) {
      return NULL;
    }
    if (haystack[i] == needle) {
      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)
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
jg     11ad <find+0x9d>
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    116e <find+0x5e>
movq   $0x0,-0x8(%rbp)
jmp    11b5 <find+0xa5>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
movsbl -0x11(%rbp),%ecx
cmp    %ecx,%eax
jne    119a <find+0x8a>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11b5 <find+0xa5>
jmp    119f <find+0x8f>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1129 <find+0x19>
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   %r12
push   %rbx
mov    %esi,%r15d
mov    %rdi,%r12
call   1030 <strlen@plt>
xor    %r14d,%r14d
test   %eax,%eax
js     1163 <find+0x53>
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%rbp
je     1163 <find+0x53>
cmp    %r15b,(%r12,%rbp,1)
je     115d <find+0x4d>
lea    0x1(%rbp),%rbx
mov    %r12,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbp
mov    %rbx,%rbp
jl     1130 <find+0x20>
jmp    1163 <find+0x53>
add    %rbp,%r12
mov    %r12,%r14
mov    %r14,%rax
pop    %rbx
pop    %r12
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
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 sle 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** %4
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = load i8, i8* %5
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %24, %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 slt i32 %3, 0
br i1 %4, label %22, label %5
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 %6, %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
}