#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 *);
char *strstr(char const *, char const *);
const char *optstr_lookup(const char *haystack, const char *needle) {
  const char *ch = haystack;
  int found = 0;
  size_t len = strlen(needle);
  while (!found) {
    ch = strstr(ch, needle);
    if (!ch) {
      break;
    }
    if (ch[len] == '\0' || ch[len] == '=' || ch[len] == ':') {
      found = 1;
    } else {
      ch++;
    }
  }
  return ch;
}


x86 assembly (clang, -O0):
<optstr_lookup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1162 <optstr_lookup+0x42>
jmp    11df <optstr_lookup+0xbf>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1183 <optstr_lookup+0x63>
jmp    11df <optstr_lookup+0xbf>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11c2 <optstr_lookup+0xa2>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x3d,%eax
je     11c2 <optstr_lookup+0xa2>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x3a,%eax
jne    11ce <optstr_lookup+0xae>
movl   $0x1,-0x1c(%rbp)
jmp    11da <optstr_lookup+0xba>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    114c <optstr_lookup+0x2c>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<optstr_lookup>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,%r14
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <strstr@plt>
test   %rax,%rax
je     1185 <optstr_lookup+0x65>
movabs $0x2400000000000001,%r15
jmp    1174 <optstr_lookup+0x54>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%rax
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strstr@plt>
test   %rax,%rax
je     1185 <optstr_lookup+0x65>
movzbl (%rax,%r14,1),%ecx
cmp    $0x3d,%rcx
ja     1160 <optstr_lookup+0x40>
bt     %rcx,%r15
jae    1160 <optstr_lookup+0x40>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @optstr_lookup(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
store i8* %8, i8** %5
store i32 0, i32* %6
%9 = load i8*, i8** %4
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %7
br label %11
11: ; preds = %47, %2
%12 = load i32, i32* %6
%13 = icmp ne i32 %12, 0
%14 = xor i1 %13, true
br i1 %14, label %15, label %48
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = load i8*, i8** %4
%18 = call i8* @strstr(i8* %16, i8* %17)
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = icmp ne i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %15
br label %48
22: ; preds = %15
%23 = load i8*, i8** %5
%24 = load i64, i64* %7
%25 = getelementptr inbounds i8, i8* %23, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 0
br i1 %28, label %43, label %29
29: ; preds = %22
%30 = load i8*, i8** %5
%31 = load i64, i64* %7
%32 = getelementptr inbounds i8, i8* %30, i64 %31
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 61
br i1 %35, label %43, label %36
36: ; preds = %29
%37 = load i8*, i8** %5
%38 = load i64, i64* %7
%39 = getelementptr inbounds i8, i8* %37, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 58
br i1 %42, label %43, label %44
43: ; preds = %36, %29, %22
store i32 1, i32* %6
br label %47
44: ; preds = %36
%45 = load i8*, i8** %5
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %5
br label %47
47: ; preds = %44, %43
br label %11
48: ; preds = %21, %11
%49 = load i8*, i8** %5
ret i8* %49
}

LLVM IR (-O2):
define i8* @optstr_lookup(i8* %0, i8* nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %14, label %6
6: ; preds = %2, %10
%7 = phi i8* [ %12, %10 ], [ %4, %2 ]
%8 = getelementptr inbounds i8, i8* %7, i64 %3
%9 = load i8, i8* %8
switch i8 %9, label %10 [
i8 0, label %14
i8 61, label %14
i8 58, label %14
]
10: ; preds = %6
%11 = getelementptr inbounds i8, i8* %7, i64 1
%12 = tail call i8* @strstr(i8* dereferenceable(1) %11, i8* dereferenceable(1) %1)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %6
14: ; preds = %10, %6, %6, %6, %2
%15 = phi i8* [ %4, %2 ], [ %12, %10 ], [ %7, %6 ], [ %7, %6 ], [ %7, %6 ]
ret i8* %15
}