#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*/ fprintf(int /*<<< orphan*/, char *, char *, char *, char,
                           int, int, int);
int /*<<< orphan*/ stderr;
char *strchr(char const *, char);
int index_of(char *str, char ch, int start, int len) {
  const char *region = str + start;
  const char *p = strchr(region, ch);
  int pos;
  if (p) {
    pos = (p - region) + start;
    if (pos >= (start + len)) {
      pos = -1;
    }
  } else {
    pos = -1;
  }
  if (0)
    fprintf(stderr, "%s: index_of(\"%s\", %c, %d, %d) = %d\n", "advent7_abba",
            str, ch, start, len, pos);
  return pos;
}


x86 assembly (clang, -O0):
<index_of>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
movsbl -0x9(%rbp),%esi
call   1030 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1185 <index_of+0x75>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
movslq -0x10(%rbp),%rcx
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x10(%rbp),%ecx
add    -0x14(%rbp),%ecx
cmp    %ecx,%eax
jl     1180 <index_of+0x70>
movl   $0xffffffff,-0x2c(%rbp)
jmp    118c <index_of+0x7c>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<index_of>:
push   %rbp
push   %r14
push   %rbx
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rdi,%rbx
movslq %edx,%rax
add    %rax,%rbx
mov    %rbx,%rdi
call   1030 <strchr@plt>
test   %rax,%rax
je     1143 <index_of+0x33>
sub    %ebx,%eax
add    %ebp,%eax
add    %ebp,%r14d
cmp    %r14d,%eax
mov    $0xffffffff,%ecx
cmovl  %eax,%ecx
jmp    1148 <index_of+0x38>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@stderr = global i32 0
define i32 @index_of(i8* %0, i8 signext %1, i32 %2, i32 %3) {
%5 = sext i32 %2 to i64
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = tail call i8* @strchr(i8* dereferenceable(1) %6, i8 signext %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %18, label %9
9: ; preds = %4
%10 = ptrtoint i8* %7 to i64
%11 = ptrtoint i8* %6 to i64
%12 = sub i64 %10, %11
%13 = trunc i64 %12 to i32
%14 = add i32 %13, %2
%15 = add nsw i32 %3, %2
%16 = icmp slt i32 %14, %15
%17 = select i1 %16, i32 %14, i32 -1
br label %18
18: ; preds = %4, %9
%19 = phi i32 [ %17, %9 ], [ -1, %4 ]
ret i32 %19
}